Skip to content

Commit 8be5864

Browse files
Eusebius1920darrachequesne
authored andcommitted
[feature] Added flag forceNode to override the normal behavior of prefering Browser based implementations. (#469)
1 parent 3427e74 commit 8be5864

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ Exposed as `eio` in the browser standalone build.
225225
- `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.
226226
- `onlyBinaryUpgrades` (`Boolean`): whether transport upgrades should be restricted to transports supporting binary data (`false`)
227227
- `requestTimeout` (`Number`): Timeout for xhr-polling requests in milliseconds (`0`)
228+
- `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)
228229
- `send`
229230
- Sends a message to the server
230231
- **Parameters**

lib/socket.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ function Socket (uri, opts) {
9292
this.ca = opts.ca || null;
9393
this.ciphers = opts.ciphers || null;
9494
this.rejectUnauthorized = opts.rejectUnauthorized === undefined ? null : opts.rejectUnauthorized;
95+
this.forceNode = !!opts.forceNode;
9596

9697
// other options for Node.js client
9798
var freeGlobal = typeof global === 'object' && global;
@@ -184,7 +185,8 @@ Socket.prototype.createTransport = function (name) {
184185
ciphers: this.ciphers,
185186
rejectUnauthorized: this.rejectUnauthorized,
186187
perMessageDeflate: this.perMessageDeflate,
187-
extraHeaders: this.extraHeaders
188+
extraHeaders: this.extraHeaders,
189+
forceNode: this.forceNode
188190
});
189191

190192
return transport;

lib/transport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function Transport (opts) {
3939
this.ca = opts.ca;
4040
this.ciphers = opts.ciphers;
4141
this.rejectUnauthorized = opts.rejectUnauthorized;
42+
this.forceNode = opts.forceNode;
4243

4344
// other options for Node.js client
4445
this.extraHeaders = opts.extraHeaders;

lib/transports/websocket.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ var inherit = require('component-inherit');
99
var yeast = require('yeast');
1010
var debug = require('debug')('engine.io-client:websocket');
1111
var BrowserWebSocket = global.WebSocket || global.MozWebSocket;
12+
var NodeWebSocket;
13+
if (typeof window === 'undefined') {
14+
try {
15+
NodeWebSocket = require('ws');
16+
} catch (e) { }
17+
}
1218

1319
/**
1420
* Get either the `WebSocket` or `MozWebSocket` globals
@@ -18,9 +24,7 @@ var BrowserWebSocket = global.WebSocket || global.MozWebSocket;
1824

1925
var WebSocket = BrowserWebSocket;
2026
if (!WebSocket && typeof window === 'undefined') {
21-
try {
22-
WebSocket = require('ws');
23-
} catch (e) { }
27+
WebSocket = NodeWebSocket;
2428
}
2529

2630
/**
@@ -42,6 +46,10 @@ function WS (opts) {
4246
this.supportsBinary = false;
4347
}
4448
this.perMessageDeflate = opts.perMessageDeflate;
49+
this.usingBrowserWebSocket = BrowserWebSocket && !opts.forceNode;
50+
if (!this.usingBrowserWebSocket) {
51+
WebSocket = NodeWebSocket;
52+
}
4553
Transport.call(this, opts);
4654
}
4755

@@ -97,7 +105,7 @@ WS.prototype.doOpen = function () {
97105
}
98106

99107
try {
100-
this.ws = BrowserWebSocket ? new WebSocket(uri) : new WebSocket(uri, protocols, opts);
108+
this.ws = this.usingBrowserWebSocket ? new WebSocket(uri) : new WebSocket(uri, protocols, opts);
101109
} catch (err) {
102110
return this.emit('error', err);
103111
}
@@ -156,7 +164,7 @@ WS.prototype.write = function (packets) {
156164
for (var i = 0, l = total; i < l; i++) {
157165
(function (packet) {
158166
parser.encodePacket(packet, self.supportsBinary, function (data) {
159-
if (!BrowserWebSocket) {
167+
if (!self.usingBrowserWebSocket) {
160168
// always create a new object (GH-437)
161169
var opts = {};
162170
if (packet.options) {
@@ -175,7 +183,7 @@ WS.prototype.write = function (packets) {
175183
// have a chance of informing us about it yet, in that case send will
176184
// throw an error
177185
try {
178-
if (BrowserWebSocket) {
186+
if (self.usingBrowserWebSocket) {
179187
// TypeError is thrown when passing the second argument on Safari
180188
self.ws.send(data);
181189
} else {

0 commit comments

Comments
 (0)