pladaria / reconnecting-websocket

Reconnecting WebSocket. For Web, React Native, cli (Node.js)
MIT License
1.22k stars 197 forks source link

fix: undefined protocol in WebSocket instanciation #79

Closed blackholegalaxy closed 5 years ago

blackholegalaxy commented 6 years ago

@pladaria

Context

On certain browsers and servers, sending an undefined protocol can break the handshake as protocol is sent with undefined value.

WebSocket connection to 'ws://server/socket' failed: Error during WebSocket handshake: Sent non-empty 'Sec-WebSocket-Protocol' header but no response was received

Request URL:ws://server/socket Cache-Control:no-cache Connection:Upgrade Host:server Origin:file:// Pragma:no-cache Sec-WebSocket-Extensions:permessage-deflate; client_max_window_bits, x-webkit-deflate-frame Sec-WebSocket-Key:YNe8KJi63BdXVqI2A+m1QA== Sec-WebSocket-Protocol:undefined Sec-WebSocket-Version:13 Upgrade:websocket User-Agent:Mozilla/5.0 (Linux; Android 4.4.4; 7-inch KitKat (4.4) XHDPI Tablet Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36

Fix

When protocol is falsy, we then avoid to include it in the WebSocket constructor. It let the browser define protocol as empty string, which is valid and doesn't break anymore.

pladaria commented 5 years ago

Merged! Thank you