WebSocket-Node
WebSocket-Node copied to clipboard
The WebSocket protocol property is not always a string
The protocol
property of the W3CWebSocket object returns undefined
in some cases while browsers will return empty string instead.
From the spec, I don't think we should ever expect the protocol property to return undefined. Ie the protocol value is empty string unless the server replies with a non-null subprotocol value and then the value is updated to a new string.
I ran into this trying to re-use some browser JS code in node.js and had to create the following shim to prevent errors:
const W3CWebSocket = require('websocket').w3cwebsocket;
class WebSocketShim extends W3CWebSocket {
get protocol () {
return this._protocol === undefined ? '' : this._protocol;
}
}