一尘不染

连接到Websocket时状态为200,但这是错误的吗?

tomcat

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

"WebSocket connection to 'ws://localhost:32768/DspClusterWebServices/myHandler' failed: Unexpected response code: 200"

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

@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:

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

我不知道下一步该怎么做。我以为这会导致afterConnectionFounded(WebSocketSession
session)方法启动?代码200不好吗?


阅读 1755

收藏
2020-06-16

共1个答案

一尘不染

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

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

无法解析Origin标头值[null]

;),然后依次将您带到该链接

您必须添加.setAllowedOrigins("*")到您的addHandler()方法中,然后它才可以正常工作!

2020-06-16