Cha*_*ngh 11 java spring-mvc websocket spring-websocket java-websocket
我已经使用Spring MVC实现了Web Socket,它对我来说很好用,即从一个浏览器工作到另一个浏览器,这个浏览器使用这个代码打开.
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public HelloMessage greeting(HelloMessage message) throws Exception {
Thread.sleep(3000); // simulated delay
return message;
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以帮助我从正常的api控制器调用@SendTo("/ topic/greetings").我尝试使用它但它对我不起作用
@RequestMapping(value = "/sendMessage")
@SendTo("/topic/greetings")
public HelloMessage sendMessage() throws Exception {
return new HelloMessage((int) Math.random(), "This is Send From Server");
}
Run Code Online (Sandbox Code Playgroud)
对此有何看法?
谢谢
Cha*_*ngh 18
我找到了解决方案
@Autowired
private SimpMessagingTemplate template;
@RequestMapping(value = "/sendMessage")
public void sendMessage() throws Exception {
this.template.convertAndSend("/topic/greetings", new HelloMessage(
(int) Math.random(), "This is Send From Server"));
}
Run Code Online (Sandbox Code Playgroud)
通过使用这个我们可以发送消息来打开WebSocket.
谢谢
| 归档时间: |
|
| 查看次数: |
2350 次 |
| 最近记录: |