Socket.io code 200 Error during WebSocket handshak

2020-04-18 06:40发布

问题:

I am using socket.io with nodejs and an apache server over it. I am getting a code 200 as response, I know I must get 101.

WebSocket connection to 'wss://SITEABC.com/socket.io/?siteId=site1234567&EIO=3&transport=websocket' failed: Error during WebSocket handshake: Invalid status line

The configuration on apache is the folowing:

RewriteCond %{HTTP:Upgrade} ^Websocket [NC]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
RewriteRule .* ws://localhost:1337/{REQUEST_URI} [P]

RewriteCond %{REQUEST_METHOD} OPTIONS
RewriteRule ^(.*)$ $1 [R=200,L]

ProxyPass / http://localhost:1337/
ProxyPassReverse / http://localhost:1337/
ProxyPass /socket.io/ http://localhost:1337/socket.io/

Node is running on port 1337

回答1:

I use springboot 2 + stomp. In my case ,the reason is in WebSocketConfig,must remove .withSockJS.

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws").setAllowedOrigins("*")
                .addInterceptors(new HandshakeInterceptor())

                //--> important must remove,or 200 error.
                //.withSockJS()

                ;
    }


回答2:

This means your requests are not being proxied to your WebSocket handler, but for some other route which returns a 200.

You should check your WebSocket handler.