I get a status 200 when connecting to the websocket, but it is an error?
Solution 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!
Solution 2
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.
Solution 3
Have a look at the specification . The server should respond with 101
to signal protocol change from http
to ws
.
Solution 4
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.
Solution 5
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
Comments
-
smuggledPancakes over 3 years
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
andTomcat 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 about 9 yearsI am still not sure what to do, this link gives further explanation of the status 200 code: stackoverflow.com/questions/18190644/… Something must be wrong on the server end, but I am not sure how, I am using Tomcat 8.0.18
-
smuggledPancakes about 9 yearsJust 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!
-
Gleb about 6 yearsAnd what to do if I need both the redirect() method and the websocket?
-
Shuai Wang almost 6 years@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 almost 6 years@forguta by just delete your websocket handler...?
-
chrischeng021 over 5 yearsOh my GOD!!!!Really thanks!!! This problem has troubled me for 3 hours....
-
curiouscupcake about 4 yearsI 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 case
-
levacjeep almost 4 yearsThis helped me: github.com/jhipster/generator-jhipster/issues/9591. (I'm using jhipster)
-
Deepak Agarwal about 3 yearsI wasted 3 days ! and this works. WoW ! Thanks !