【发布时间】:2015-01-25 07:25:48
【问题描述】:
有没有办法将 WebSockets 与 SockJS 客户端和 Spring 4 服务器一起使用,但不使用 STOMP?
根据 Spring 网站上的本教程,我知道如何使用 Stomp 和 Spring 4 设置基于 WebSocket 的应用程序。在客户端,我们有:
var socket = new SockJS('/hello');
stompClient = Stomp.over(socket);
stompClient.connect({}, function(frame) {
setConnected(true);
console.log('Connected: ' + frame);
stompClient.subscribe('/topic/greetings', function(greeting){
showGreeting(JSON.parse(greeting.body).content);
});
});
在服务器端,我们在控制器中有以下内容:
@MessageMapping("/hello")
@SendTo("/topic/greetings")
public Greeting greeting(HelloMessage message) throws Exception {
Thread.sleep(3000); // simulated delay
return new Greeting("Hello, " + message.getName() + "!");
}
现在,我知道@MessageMapping("/hello") 确保如果将消息发送到目标"/hello",则将调用greeting() 方法。由于stompClient 订阅了"/topic/greetings",@SendTo("/topic/greetings") 会将消息发送回stompClient。
但是上面的问题是 stompClient 是一个 Stomp 对象。我想简单地使用sock.send('test'); 并将其传送到我的服务器的目的地。而我想做@SendTo("myownclientdestinationmap"),可以通过
sock.onmessage = function(e) {
console.log('message', e.data);
};
那么,有什么方法可以使用 Spring 4、SockJS 而没有 Stomp?还是 Spring 4 WebSocket 只支持 Stomp?
【问题讨论】:
-
我想在我的
spring-mvc应用程序中使用WebSocket,你能指导/告诉我为什么你不想使用STOMP吗?谢谢!!!
标签: java websocket sockjs spring-4