WebSocket握手-意外的响应代码200-AngularJs和Spring Boot

Her*_*vic 7 java spring websocket angularjs

当我尝试在AngularJS应用和Spring Boot之间建立websocket通信时,出现错误:websocket握手期间出错-意外的响应代码:200

这是我的JS代码:

function run(stateHandler, translationHandler, $websocket) {
    stateHandler.initialize();
    translationHandler.initialize();

    var ws = $websocket.$new('ws://localhost:8080/socket'); // instance of ngWebsocket, handled by $websocket service

    ws.$on('$open', function () {
        console.log('Oh my gosh, websocket is really open! Fukken awesome!');  
    });

    ws.$on('/topic/notification', function (data) {
        console.log('The websocket server has sent the following data:');
        console.log(data);

        ws.$close();
    });

    ws.$on('$close', function () {
        console.log('Noooooooooou, I want to have more fun with ngWebsocket, damn it!');
    });
}
Run Code Online (Sandbox Code Playgroud)

这是我的Java代码:

WebsocketConfiguration.java

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
{
    stompEndpointRegistry.addEndpoint("/socket")
        .setAllowedOrigins("*")
        .withSockJS();
}

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic");
}
Run Code Online (Sandbox Code Playgroud)

WebsocketSecurityConfiguration.java

@Override
protected void configureInbound(MessageSecurityMetadataSourceRegistry messages) {
    messages
        // message types other than MESSAGE and SUBSCRIBE
        .nullDestMatcher().authenticated()
        // matches any destination that starts with /rooms/
        .simpDestMatchers("/topic/tracker").hasAuthority(AuthoritiesConstants.ADMIN)
        .simpDestMatchers("/topic/**").permitAll()
        // (i.e. cannot send messages directly to /topic/, /queue/)
        // (i.e. cannot subscribe to /topic/messages/* to get messages sent to
        // /topic/messages-user<id>)
        .simpTypeMatchers(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE).denyAll()
        // catch all
        .anyMessage().denyAll();
}
Run Code Online (Sandbox Code Playgroud)

有谁知道如何解决这个问题?
先感谢您!

Luk*_*rić 20

我有一个类似的问题,我正在使用chrome插件(简单的WebSocket客户端)测试我的Websocket连接,并尝试连接到我的代码中定义的ws:// localhost:8080 / handler /, registry.addEndpoint("/handler").setAllowedOrigins("*").withSockJS();但发生了意外错误200 。我已通过将/ websocket附加到chrome扩展程序上的客户端请求字符串中来解决此问题,因此您可以尝试在JS文件中更改以下行:

var ws = $websocket.$new('ws://localhost:8080/socket');
Run Code Online (Sandbox Code Playgroud)

 var ws = $websocket.$new('ws://localhost:8080/socket/websocket');
Run Code Online (Sandbox Code Playgroud)

我不知道为什么在我的情况下解决这个问题,我只是偶然发现了它,如果some1可以进一步阐明它,那将是非常好的:)

  • 这是正确的答案。当你使用 withSockJS() 时,你必须将 /websocket 添加到你的 url 中。如果没有 SockJS,您必须使用 WebSocketConfiguration 中的 url。 (4认同)
  • 我发现使用.withSockJS();配置spring;意味着必须添加“ websocket”。我想是因为您要提供后备选项?如果删除.withSockJS(); 您不需要附加websocket。谢谢您的回答! (2认同)
  • @PeterCatalin您可以在链接&gt; https://github.com/sockjs/sockjs-node#connecting-to-sockjs-node-without-the-client 检查后缀 /websocket (2认同)

小智 5

你可以尝试这个WebsocketConfiguration配置:

@Override
public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
{
    stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*");      
    stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
}
Run Code Online (Sandbox Code Playgroud)

那么你有 websocket 和 SockJS 配置吗?

  • .withSockJS() 应该将端点配置为当 websocket 在浏览器中不可用时回退到 SockJS。但是,如果您使用 .withSockJS() ,那么您似乎还需要将 Web 客户端设置为使用它,否则您将在协商期间获得 200 状态。比较:https://stomp-js.github.io/guide/stompjs/rx-stomp/ng2-stompjs/using-stomp-with-sockjs.html#example-with-stompjs 与 https://stomp-js。 github.io/guide/stompjs/using-stompjs-v5.html#create-a-stomp-client。 (2认同)