Skip to content

Commit 26e9329

Browse files
cthayerdarrachequesne
authored andcommitted
[feat] Detect React-Native environment and use all websocket features (#591)
React-Native provides a Websocket object that is more functionally aligned with the Node.js websocket than the browser websocket. It has the same constructor signature as the Node.js websocket and can support extraHeaders and protocols. This PR will detect when the engine.io-client is running in React-Native, call the proper Websocket constructor, and enable support for extraHeaders.
1 parent 28765c5 commit 26e9329

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ build
44
components
55
/coverage
66
npm-debug.log
7+
.idea

lib/socket.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,12 @@ function Socket (uri, opts) {
9494
this.rejectUnauthorized = opts.rejectUnauthorized === undefined ? true : opts.rejectUnauthorized;
9595
this.forceNode = !!opts.forceNode;
9696

97-
// other options for Node.js client
97+
// detect ReactNative environment
98+
this.isReactNative = (typeof navigator !== 'undefined' && typeof navigator.product === 'string' && navigator.product.toLowerCase() === 'reactnative');
99+
100+
// other options for Node.js or ReactNative client
98101
var freeGlobal = typeof global === 'object' && global;
99-
if (freeGlobal.global === freeGlobal) {
102+
if (freeGlobal.global === freeGlobal || this.isReactNative) {
100103
if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
101104
this.extraHeaders = opts.extraHeaders;
102105
}
@@ -196,7 +199,8 @@ Socket.prototype.createTransport = function (name) {
196199
forceNode: options.forceNode || this.forceNode,
197200
localAddress: options.localAddress || this.localAddress,
198201
requestTimeout: options.requestTimeout || this.requestTimeout,
199-
protocols: options.protocols || void (0)
202+
protocols: options.protocols || void (0),
203+
isReactNative: this.isReactNative
200204
});
201205

202206
return transport;

lib/transport.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ function Transport (opts) {
4141
this.rejectUnauthorized = opts.rejectUnauthorized;
4242
this.forceNode = opts.forceNode;
4343

44+
// results of ReactNative environment detection
45+
this.isReactNative = opts.isReactNative;
46+
4447
// other options for Node.js client
4548
this.extraHeaders = opts.extraHeaders;
4649
this.localAddress = opts.localAddress;

lib/transports/websocket.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ WS.prototype.doOpen = function () {
109109
}
110110

111111
try {
112-
this.ws = this.usingBrowserWebSocket ? (protocols ? new WebSocket(uri, protocols) : new WebSocket(uri)) : new WebSocket(uri, protocols, opts);
112+
this.ws = this.usingBrowserWebSocket && !this.isReactNative ? (protocols ? new WebSocket(uri, protocols) : new WebSocket(uri)) : new WebSocket(uri, protocols, opts);
113113
} catch (err) {
114114
return this.emit('error', err);
115115
}

0 commit comments

Comments
 (0)