WebSocket Handshake - Unexpected response code 200 - AngularJs and Spring Boot

后端 未结 3 1608
生来不讨喜
生来不讨喜 2020-12-31 07:54

When I tried to establish websocket communication between AngularJS app and Spring Boot I\'m getting the error: Error during websocket handshake - Unexpected respons

相关标签:
3条回答
  • 2020-12-31 08:37

    I had a similiar problem, I was testing my websocket connection using an chrome plugin (Simple WebSocket Client) and was trying to connect to ws://localhost:8080/handler/ which is defined in my code as registry.addEndpoint("/handler").setAllowedOrigins("*").withSockJS(); but unexpected error 200 was occuring. I've fixed this by appending /websocket to my client request string on the chrome extension, so what you could try is to change in your JS file the following line:

    var ws = $websocket.$new('ws://localhost:8080/socket');
    

    to

     var ws = $websocket.$new('ws://localhost:8080/socket/websocket');
    

    I dont know the reason why this fixed it in my case i just randomly stumbled upon it, if some1 could clarify it more it would be really nice :)

    0 讨论(0)
  • 2020-12-31 08:41

    Can you try this WebsocketConfiguration configuration:

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry)
    {
        stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*");      
        stompEndpointRegistry.addEndpoint("/socket").setAllowedOrigins("*").withSockJS();
    }
    

    so you have both websocket and SockJS configurations?

    0 讨论(0)
  • 2020-12-31 08:49

    You need to use a sockJS client if you configure it to use sockJS on your websocket endpoint.

    0 讨论(0)
提交回复
热议问题