Skip to content

Commit

Permalink
[feat] Allow to set the protocols for the websocket transport (#546)
Browse files Browse the repository at this point in the history
Some WebSocket implementations require the protocols parameter or will
fail connection.
  • Loading branch information
stormbkk87 authored and darrachequesne committed Mar 6, 2017
1 parent be4c906 commit 1519765
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,12 @@ Exposed as `eio` in the browser standalone build.
- `threshold` (`Number`): data is compressed only if the byte size is above this value. This option is ignored on the browser. (`1024`)
- `extraHeaders` (`Object`): Headers that will be passed for each request to the server (via xhr-polling and via websockets). These values then can be used during handshake or for special proxies. Can only be used in Node.js client environment.
- `onlyBinaryUpgrades` (`Boolean`): whether transport upgrades should be restricted to transports supporting binary data (`false`)
- `requestTimeout` (`Number`): Timeout for xhr-polling requests in milliseconds (`0`)
- `forceNode` (`Boolean`): Uses NodeJS implementation for websockets - even if there is a native Browser-Websocket available, which is preferred by default over the NodeJS implementation. (This is useful when using hybrid platforms like nw.js or electron) (`false`, NodeJS only)
- `localAddress` (`String`): the local IP address to connect to
- **Polling-only options**
- `requestTimeout` (`Number`): Timeout for xhr-polling requests in milliseconds (`0`)
- **Websocket-only options**
- `protocols` (`Array`): a list of subprotocols (see [MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Subprotocols))
- `send`
- Sends a message to the server
- **Parameters**
Expand Down
3 changes: 2 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ Socket.prototype.createTransport = function (name) {
extraHeaders: options.extraHeaders || this.extraHeaders,
forceNode: options.forceNode || this.forceNode,
localAddress: options.localAddress || this.localAddress,
requestTimeout: options.requestTimeout || this.requestTimeout
requestTimeout: options.requestTimeout || this.requestTimeout,
protocols: options.protocols || void (0)
});

return transport;
Expand Down
5 changes: 3 additions & 2 deletions lib/transports/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ function WS (opts) {
}
this.perMessageDeflate = opts.perMessageDeflate;
this.usingBrowserWebSocket = BrowserWebSocket && !opts.forceNode;
this.protocols = opts.protocols;
if (!this.usingBrowserWebSocket) {
WebSocket = NodeWebSocket;
}
Expand Down Expand Up @@ -86,7 +87,7 @@ WS.prototype.doOpen = function () {
}

var uri = this.uri();
var protocols = void (0);
var protocols = this.protocols;
var opts = {
agent: this.agent,
perMessageDeflate: this.perMessageDeflate
Expand All @@ -108,7 +109,7 @@ WS.prototype.doOpen = function () {
}

try {
this.ws = this.usingBrowserWebSocket ? new WebSocket(uri) : new WebSocket(uri, protocols, opts);
this.ws = this.usingBrowserWebSocket ? (protocols ? new WebSocket(uri, protocols) : new WebSocket(uri)) : new WebSocket(uri, protocols, opts);
} catch (err) {
return this.emit('error', err);
}
Expand Down

0 comments on commit 1519765

Please sign in to comment.