WebSocket 握手 - 意外响应代码 200 - AngularJs 和 Spring Boot [英] WebSocket Handshake - Unexpected response code 200 - AngularJs and Spring Boot

查看:38
本文介绍了WebSocket 握手 - 意外响应代码 200 - AngularJs 和 Spring Boot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的JS代码:

function run(stateHandler, translationHandler, $websocket) {stateHandler.initialize();translationHandler.initialize();var ws = $websocket.$new('ws://localhost:8080/socket');//ngWebsocket 实例,由 $websocket 服务处理ws.$on('$open', function () {console.log('天哪,websocket 真的打开了!Fukken 太棒了!');});ws.$on('/topic/notification', function (data) {console.log('Websocket 服务器发送了以下数据:');控制台日志(数据);ws.$close();});ws.$on('$close', function () {console.log('Noooooooooou,我想用 ngWebsocket 玩得更开心,该死!');});}

这是我的 Java 代码:

<块引用>

WebsocketConfiguration.java

@Override公共无效 registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry){stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();}@覆盖public void configureMessageBroker(MessageBrokerRegistry 注册表){registry.enableSimpleBroker("/topic");}

<块引用>

WebsocketSecurityConfiguration.java

@Overrideprotected void configureInbound(MessageSecurityMetadataSourceRegistry 消息) {消息//除 MESSAGE 和 SUBSCRIBE 之外的消息类型.nu​​llDestMatcher().authenticated()//匹配任何以/rooms/开头的目的地.simpDestMatchers("/topic/tracker").hasAuthority(AuthoritiesConstants.ADMIN).simpDestMatchers("/topic/**").permitAll()//(即不能直接向/topic/、/queue/发送消息)//(即不能订阅/topic/messages/* 来获取发送到的消息///topic/messages-user).simpTypeMatchers(SimpMessageType.MESSAGE, SimpMessageType.SUBSCRIBE).denyAll()//捕获所有.anyMessage().denyAll();}

有人知道如何解决这个问题吗?
提前谢谢你!

解决方案

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

var ws = $websocket.$new('ws://localhost:8080/socket');

 var ws = $websocket.$new('ws://localhost:8080/socket/websocket');

我不知道为什么在我的情况下修复了它,我只是偶然发现了它,如果有人能更清楚地说明它,那就太好了:)

When I tried to establish websocket communication between AngularJS app and Spring Boot I'm getting the error: Error during websocket handshake - Unexpected response code: 200.

Here is my JS code:

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!');
    });
}

And here is my Java code:

WebsocketConfiguration.java

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

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.enableSimpleBroker("/topic");
}

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

Does anyone have an idea how to fix this problem?
Thank you in advance!

解决方案

I had a similiar problem, I was testing my websocket connection using an chrome plugin (Simple WebSocket Client) and was trying to connect to ws://localhost:8080/handler/ which is defined in my code as registry.addEndpoint("/handler").setAllowedOrigins("*").withSockJS(); but unexpected error 200 was occuring. I've fixed this by appending /websocket to my client request string on the chrome extension, so what you could try is to change in your JS file the following line:

var ws = $websocket.$new('ws://localhost:8080/socket');

to

 var ws = $websocket.$new('ws://localhost:8080/socket/websocket');

I dont know the reason why this fixed it in my case i just randomly stumbled upon it, if some1 could clarify it more it would be really nice :)

这篇关于WebSocket 握手 - 意外响应代码 200 - AngularJs 和 Spring Boot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆