Skip to content

Commit 1519765

Browse files
stormbkk87darrachequesne
authored andcommitted
[feat] Allow to set the protocols for the websocket transport (#546)
Some WebSocket implementations require the protocols parameter or will fail connection.
1 parent be4c906 commit 1519765

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,12 @@ Exposed as `eio` in the browser standalone build.
225225
- `threshold` (`Number`): data is compressed only if the byte size is above this value. This option is ignored on the browser. (`1024`)
226226
- `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.
227227
- `onlyBinaryUpgrades` (`Boolean`): whether transport upgrades should be restricted to transports supporting binary data (`false`)
228-
- `requestTimeout` (`Number`): Timeout for xhr-polling requests in milliseconds (`0`)
229228
- `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)
230229
- `localAddress` (`String`): the local IP address to connect to
230+
- **Polling-only options**
231+
- `requestTimeout` (`Number`): Timeout for xhr-polling requests in milliseconds (`0`)
232+
- **Websocket-only options**
233+
- `protocols` (`Array`): a list of subprotocols (see [MDN reference](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#Subprotocols))
231234
- `send`
232235
- Sends a message to the server
233236
- **Parameters**

lib/socket.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ Socket.prototype.createTransport = function (name) {
196196
extraHeaders: options.extraHeaders || this.extraHeaders,
197197
forceNode: options.forceNode || this.forceNode,
198198
localAddress: options.localAddress || this.localAddress,
199-
requestTimeout: options.requestTimeout || this.requestTimeout
199+
requestTimeout: options.requestTimeout || this.requestTimeout,
200+
protocols: options.protocols || void (0)
200201
});
201202

202203
return transport;

lib/transports/websocket.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ function WS (opts) {
4747
}
4848
this.perMessageDeflate = opts.perMessageDeflate;
4949
this.usingBrowserWebSocket = BrowserWebSocket && !opts.forceNode;
50+
this.protocols = opts.protocols;
5051
if (!this.usingBrowserWebSocket) {
5152
WebSocket = NodeWebSocket;
5253
}
@@ -86,7 +87,7 @@ WS.prototype.doOpen = function () {
8687
}
8788

8889
var uri = this.uri();
89-
var protocols = void (0);
90+
var protocols = this.protocols;
9091
var opts = {
9192
agent: this.agent,
9293
perMessageDeflate: this.perMessageDeflate
@@ -108,7 +109,7 @@ WS.prototype.doOpen = function () {
108109
}
109110

110111
try {
111-
this.ws = this.usingBrowserWebSocket ? new WebSocket(uri) : new WebSocket(uri, protocols, opts);
112+
this.ws = this.usingBrowserWebSocket ? (protocols ? new WebSocket(uri, protocols) : new WebSocket(uri)) : new WebSocket(uri, protocols, opts);
112113
} catch (err) {
113114
return this.emit('error', err);
114115
}

0 commit comments

Comments
 (0)