带有 Sockjs & 的 WebSocketSpring 4 但没有 Stomp [英] WebSocket with Sockjs & Spring 4 but without Stomp
问题描述
有没有办法将 WebSockets 与 SockJS 客户端和 Spring 4 服务器一起使用,但不使用 STOMP?
Is there a way to use WebSockets with SockJS client and Spring 4 server but not using STOMP?
根据 Spring 网站上的本教程,我知道如何使用 Stomp 和 Spring 4 设置基于 WebSocket 的应用程序.在客户端,我们有:
Based on this tutorial from Spring's website, I know how to set up a WebSocket based application using Stomp and Spring 4. On the client side, we have:
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);
});
});
在服务器端,我们在控制器中有以下内容:
And on the server side, we have the following in the controller:
@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
.
Now, I understand that @MessageMapping("/hello")
ensures that if a message is sent to a destination "/hello"
, then the greeting()
method will be called. And since the stompClient
is subscribed to "/topic/greetings"
, the @SendTo("/topic/greetings")
will send the message back to the stompClient
.
但是上面的问题是 stompClient 是一个 Stomp 对象.我想简单地使用 sock.send('test');
并将其发送到我的服务器的目的地.我想做@SendTo("myownclientdestinationmap")
,我可以通过
But the problem with the above is that stompClient is a Stomp object. And I want to simply use sock.send('test');
and have it delivered to my server's destination. And I want to do @SendTo("myownclientdestinationmap")
, I can receive it by
sock.onmessage = function(e) {
console.log('message', e.data);
};
那么,在没有 Stomp 的情况下,有什么办法可以用 Spring 4、SockJS 做到这一点?还是 Spring 4 WebSocket 只支持 Stomp?
So, any way to do this with Spring 4, SockJS and without Stomp? Or does Spring 4 WebSocket only supports Stomp?
推荐答案
Spring 支持 STOMP
over WebSocket
但子协议的使用不是强制性的强>,你可以处理原始的websocket.使用原始 websocket 时,发送的消息缺少信息,无法使 Spring 将其路由到特定的消息处理程序方法(我们没有任何消息传递协议),因此您不必注释控制器,而是必须实现 WebSocketHandler
:
Spring supports STOMP
over WebSocket
but the use of a subprotocol is not mandatory, you can deal with the raw websocket. When using a raw websocket, the message sent lacks of information to make Spring route it to a specific message handler method (we don't have any messaging protocol), so instead of annotating your controller, you'll have to implement a WebSocketHandler
:
public class GreetingHandler extends TextWebSocketHandler {
@Override
public void handleTextMessage(WebSocketSession session, TextMessage message) {
Thread.sleep(3000); // simulated delay
TextMessage msg = new TextMessage("Hello, " + message.getPayload() + "!");
session.sendMessage(msg);
}
}
然后将您的处理程序添加到配置中的注册表中(您可以添加多个处理程序并使用 SockJS
作为后备选项):
And then add your handler to the registry in the configuration (you can add more than one handler and use SockJS
for fallback options):
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
registry.addHandler(greetingHandler(), "/greeting").withSockJS();
}
@Bean
public WebSocketHandler greetingHandler() {
return new GreetingHandler();
}
}
客户端将是这样的:
var sock = new SockJS('http://localhost:8080/greeting');
sock.onmessage = function(e) {
console.log('message', e.data);
}
这篇关于带有 Sockjs & 的 WebSocketSpring 4 但没有 Stomp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!