I get a status 200 when connecting to the websocket, but it is an error?

痞子三分冷 提交于 2019-11-26 13:49:31

问题


My error shows up in the console of my browser:

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

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

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

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

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?


回答1:


Please check http://procbits.com/connecting-to-a-sockjs-server-from-native-html5-websocket!

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

Failed to parse Origin header value [null]

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

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




回答2:


Have a look at the specification . The server should respond with 101 to signal protocol change from http to ws.




回答3:


As my another answer:[https://stackoverflow.com/a/53272666/2930417][1]

I use springboot 2 +STOMP。

remove .withSockJS(),then everything is ok.

I don't know the reason,but works for me.




回答4:


For those guys like me who use angular + springboot and got this error. please check if you have enabled the redirect or forward all non api endpoint request back to index.html. like:

@RequestMapping(value = "/**/{[path:[^\\.]*}")
    public String redirect() {
        // Forward to home page so that route is preserved.
        return "forward:/index.html";
    }

If you do, disable it and you will get 101




回答5:


Don't know if this is too late but a solution that I stumbled upon is simply appending the string /websocket after the websocket endpoint that you declared in the spring boot server. This will help keep both the forwarding logic and connect and establish a websocket connection.




回答6:


Please check that if 'ws://localhost:32768/DspClusterWebServices/myHandler' is correct.



来源:https://stackoverflow.com/questions/29829597/i-get-a-status-200-when-connecting-to-the-websocket-but-it-is-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!