From 151976594ef7830deeadfc5c68f265eca2bdfb17 Mon Sep 17 00:00:00 2001 From: stormbkk87 Date: Mon, 6 Mar 2017 19:07:19 +0700 Subject: [PATCH] [feat] Allow to set the protocols for the websocket transport (#546) Some WebSocket implementations require the protocols parameter or will fail connection. --- README.md | 5 ++++- lib/socket.js | 3 ++- lib/transports/websocket.js | 5 +++-- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c838ed216..7e335a360 100644 --- a/README.md +++ b/README.md @@ -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** diff --git a/lib/socket.js b/lib/socket.js index 84cb6cd33..9bf9f4607 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -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; diff --git a/lib/transports/websocket.js b/lib/transports/websocket.js index 0a48f1497..de14ba005 100644 --- a/lib/transports/websocket.js +++ b/lib/transports/websocket.js @@ -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; } @@ -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 @@ -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); }