简体   繁体   English

WebSocket与Sockjs和Spring 4但没有Stomp

[英]WebSocket with Sockjs & Spring 4 but without Stomp

Is there a way to use WebSockets with SockJS client and Spring 4 server but not using STOMP? 有没有办法将WebSockets与SockJS客户端和Spring 4服务器一起使用但不使用STOMP?

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: 基于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);
            });
        });

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() + "!");
}

Now, I understand that @MessageMapping("/hello") ensures that if a message is sent to a destination "/hello" , then the greeting() method will be called. 现在,我知道@MessageMapping("/hello")确保如果将消息发送到目的地"/hello" ,则将调用greeting()方法。 And since the stompClient is subscribed to "/topic/greetings" , the @SendTo("/topic/greetings") will send the message back to the stompClient . 由于stompClient订阅了"/topic/greetings"@SendTo("/topic/greetings")会将消息发送回stompClient

But the problem with the above is that stompClient is a Stomp object. 但是上面的问题是stompClient是一个Stomp对象。 And I want to simply use sock.send('test'); 我想简单地使用sock.send('test'); and have it delivered to my server's destination. 并将它发送到我的服务器的目的地。 And I want to do @SendTo("myownclientdestinationmap") , I can receive it by 我想做@SendTo("myownclientdestinationmap") ,我可以收到它

sock.onmessage = function(e) {
     console.log('message', e.data);
 };

So, any way to do this with Spring 4, SockJS and without Stomp? 那么,使用Spring 4,SockJS和没有Stomp的任何方法都可以做到这一点? Or does Spring 4 WebSocket only supports Stomp? 或者Spring 4 WebSocket只支持Stomp?

Spring supports STOMP over WebSocket but the use of a subprotocol is not mandatory , you can deal with the raw websocket. Spring通过WebSocket支持STOMP ,但子协议的使用不是强制性的 ,您可以处理原始的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 : 当使用原始websocket时,发送的消息缺少信息以使Spring将其路由到特定的消息处理程序方法(我们没有任何消息传递协议),因此您不必注释控制器,而是必须实现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);
    }
}

And then add your handler to the registry in the configuration (you can add more than one handler and use SockJS for fallback options): 然后将您的处理程序添加到配置中的注册表中(您可以添加多个处理程序并使用SockJS作为后备选项):

@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();
    }
}

The client side will be something like this: 客户端将是这样的:

var sock = new SockJS('http://localhost:8080/greeting');

sock.onmessage = function(e) {
    console.log('message', e.data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM