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?

smuggledPancakes
  • 9,881
  • 20
  • 74
  • 113
  • 1
    take a look at this, plz http://stackoverflow.com/a/14555549/592355 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." – xerx593 Apr 23 '15 at 17:42
  • 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... – smuggledPancakes Apr 23 '15 at 17:47
  • How about the Controller annotation? (It is not in WebSocket samples)...dont you think that could interfere? – xerx593 Apr 23 '15 at 17:55
  • Removing the @Controller had no effect, I still get an error code of 200 – smuggledPancakes Apr 23 '15 at 17:58
  • 1
    hummm...[this](http://procbits.com/connecting-to-a-sockjs-server-from-native-html5-websocket) sounds good: please try to connect to "ws://localhost:8080/myApp/myHandler/websocket" !? – xerx593 Apr 23 '15 at 18:05
  • 1
    xerx593 please make an answer containing that link and also with this link: https://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 deserve – smuggledPancakes Apr 23 '15 at 18:29

6 Answers6

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!

xerx593
  • 12,237
  • 5
  • 33
  • 64
  • 16
    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! – smuggledPancakes Apr 23 '15 at 19:00
  • Oh my GOD!!!!Really thanks!!! This problem has troubled me for 3 hours.... – chrischeng021 Jan 07 '19 at 07:10
  • I wasted 3 days ! and this works. WoW ! Thanks ! – Deepak Agarwal Mar 04 '21 at 02:51
19

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.

hatanooh
  • 3,891
  • 1
  • 14
  • 9
  • I followed the official tutorial of https://spring.io/guides/gs/messaging-stomp-websocket/ and add .withSockJS(). Removing it helped since I only want to use the library StompJS in my case – curiouscupcake Mar 02 '20 at 10:34
10

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

Community
  • 1
  • 1
Hannes
  • 2,018
  • 25
  • 32
  • I am still not sure what to do, this link gives further explanation of the status 200 code: http://stackoverflow.com/questions/18190644/what-does-websocket-upgrade-request-failed-exception-mean-in-a-context-of-web Something must be wrong on the server end, but I am not sure how, I am using Tomcat 8.0.18 – smuggledPancakes Apr 23 '15 at 17:04
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.

Henry
  • 133
  • 2
  • 5
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

Shuai Wang
  • 142
  • 1
  • 7
  • And what to do if I need both the redirect() method and the websocket? – Gleb Apr 26 '18 at 12:33
  • 1
    @Gleb 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/**"}) – Shuai Wang May 29 '18 at 17:26
  • @forguta by just delete your websocket handler...? – Shuai Wang May 29 '18 at 17:32
  • This helped me: https://github.com/jhipster/generator-jhipster/issues/9591. (I'm using jhipster) – levacjeep May 18 '20 at 22:02
2

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

Yang Peiyong
  • 11,536
  • 2
  • 21
  • 15