During the WebSocket handshake, we’ve got unexpected response code 403, which means Forbidden. Then SockJS attempts to connect with the alternate transports, like xhr_streaming
, but still got the 403 code. This is because we try to connect from outside of localhost and our request doesn’t pass access control check. To enable CORS we can simple add allowed origins in WebSocketConfig
:
7 Comments
CPicou
Thank you, you solved my problem for the connection thanks to setAllowedOrigins.
But now, my problem is that the client don’t receive the message. I’ve put a breakpoint in the controller and it’s ok. So it seems that the subscription doesn’t work. Do you have any idea ?
kojot
Hello CPicau, thanks for the feedback. First, please clone my project https://github.com/kkojot/spring-vue-websocket-stomp and check that you still have the problem. If so, please look into the browser console and paste the log here. If you see the correct `HelloMessage` value in the `greeting` method of the controller, it means that the connection and Jackson parsing works fine. The problem must be in Subscription URL, `configureMessageBroker` or `@SendTo` annotation. And did you try to test it with Apic either?
CPicou
I’ve found the problem ! I was using the annotation @SendToUser instead of @SendTo. It comes from another tutorial…
https://www.baeldung.com/spring-websockets-sendtouser
Thank you very much !
edwinJJ
It was a very helpful to me
Thank you 🙂
Pintec
Thank you, that was useful indeed!
There wasn’t much to find on this topic 🙂
JoeyBagOfDonuts
Thank you! Very helpful. But am having a problem when I connect. Looking at the console log, I get “Opening web socket…”, then “Woops! Lost connection to…”… Any troubleshooting tips?
Anonymous
Thank you so much 🙂