连接到 websocket 时我得到状态 200,但这是一个错误? [英] I get a status 200 when connecting to the websocket, but it is an error?

查看:98
本文介绍了连接到 websocket 时我得到状态 200,但这是一个错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的错误显示在浏览器的控制台中:

My error shows up in the console of my browser:

WebSocket 连接到 'ws://localhost:32768/DspClusterWebServices/myHandler' 失败:意外响应代码:200"

我正在使用 Spring Websockets 4.1.5Tomcat 8.0.18.我的 WebSocketConfigurer 实现类如下所示:

I am using Spring Websockets 4.1.5 and Tomcat 8.0.18. My WebSocketConfigurer implementation class looks like:

@Configuration
@Controller
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer
{
   class MyHandler implements WebSocketHandler
   {
      @Override
      public void afterConnectionEstablished(WebSocketSession session) throws Exception
      {
         System.out.println("afterConntectionEstablished called");
      }

       ...implements rest of functions with a System.out.println and false for supportsPartialMessages()

      }
   }

   @Override registerWebSocketHandlers(WebSocketHandlerRegistry registry)
   {
      registry.addHandler(myHandler(), "myHandler").withSockJS();
   }

   @Bean
   public WebSocketHandler myHandler()
   {
      return new MyHandler();
   }
}

我的 testWebsocketClient.js 尝试使用此代码连接,但错误代码为 200:

My testWebsocketClient.js tries to connect with this code, but has a error code of 200:

websocket = new WebSocket("ws://localhost:8080/myApp/myHandler");

我不知道接下来要尝试什么.我认为这会导致 afterConnectionEstablished(WebSocketSession session) 方法触发?代码 200 不是很好吗?

I cannot figure out what to try next. I thought that this would cause the afterConnectionEstablished(WebSocketSession session) method to fire? Isn't code 200 good?

推荐答案

请查看 http://procbits.com/connecting-to-a-sockjs-server-from-native-html5-websocket

在您附加 /websocket(到您的 URL)后,它会给您错误

After you append /websocket (to your URL), it will give you the error

无法解析 Origin 标头值 [null]

Failed to parse Origin header value [null]

;),然后将引导您到该链接.

;) , which then will in turn lead you to that link.

您必须将 .setAllowedOrigins("*") 添加到您的 addHandler() 方法中,然后它终于可以工作了!

You'll have to add .setAllowedOrigins("*") to your addHandler() method, and then it could finally work!

这篇关于连接到 websocket 时我得到状态 200,但这是一个错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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