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

6

39

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?

Findlay answered 23/4, 2015 at 16:40 Comment(6)
take a look at this, plz https://mcmap.net/q/409343/-connecting-to-html5-websocket in particular: "As for your "Unexpected response code: 200" error, I'm guessing that the WebSocket URL you're using on the client side is not pointing to a valid server-side script, but that's hard to comment on without more info."Procession
What information is needed? I am using Tomcat 8.0.18 and I can see the websocket-api.jar and tomcat-websocket.jar in the lib folder of the Tomcat installation...Findlay
How about the Controller annotation? (It is not in WebSocket samples)...dont you think that could interfere?Procession
Removing the @Controller had no effect, I still get an error code of 200Findlay
hummm...this sounds good: please try to connect to "ws://localhost:8080/myApp/myHandler/websocket" !?Procession
xerx593 please make an answer containing that link and also with this link: github.com/spring-projects/spring-boot/issues/2586 After I added websocket, it gave me the error "Failed to parse Origin header value [null]", which then lead me to that link. I had to add .setAllowedOrigins("*") to my addHandler() method. Then it finally worked! If you make the answer I will check it so you get the credit you deserveFindlay
P
47

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!

Procession answered 23/4, 2015 at 18:37 Comment(3)
Just as an add-on, if you remove the .withSockJs() call off the server and the /websocket off the client code, it will work that way too! Tricky stuff, hopefully this helps people in the future! Thanks again xerx593 for helping me find a solution!Findlay
Oh my GOD!!!!Really thanks!!! This problem has troubled me for 3 hours....Almaalmaata
I wasted 3 days ! and this works. WoW ! Thanks !Instruct
X
19

As my another answer:[https://mcmap.net/q/409344/-socket-io-code-200-error-during-websocket-handshake][1]

I use springboot 2 +STOMP。

remove .withSockJS(),then everything is ok.

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

Xerosis answered 13/11, 2018 at 2:0 Comment(1)
I followed the official tutorial of spring.io/guides/gs/messaging-stomp-websocket and add .withSockJS(). Removing it helped since I only want to use the library StompJS in my caseLizethlizette
J
10

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

Jaimeejaimes answered 23/4, 2015 at 16:49 Comment(1)
I am still not sure what to do, this link gives further explanation of the status 200 code: #18191144 Something must be wrong on the server end, but I am not sure how, I am using Tomcat 8.0.18Findlay
S
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.

Se answered 5/11, 2018 at 13:27 Comment(0)
Q
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

Quartz answered 22/2, 2018 at 16:43 Comment(4)
And what to do if I need both the redirect() method and the websocket?Kell
@Kell put all the route path you defined at angular level in the value array. for example, you defined "/login", "/logout" and "/welcome" routes in app.module.ts, and you also have some restful calls starting with "api". then you need to modify the annotation like @RequestMapping(value = {"/login", "/logout", "/welcome", "/api/**"})Quartz
@forguta by just delete your websocket handler...?Quartz
This helped me: github.com/jhipster/generator-jhipster/issues/9591. (I'm using jhipster)Trafalgar
B
2

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

Bannister answered 8/8, 2016 at 7:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.