Skip to content

Commit

Permalink
[feat] Detect React-Native environment and use all websocket features (
Browse files Browse the repository at this point in the history
…#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.
  • Loading branch information
cthayer authored and darrachequesne committed Nov 2, 2018
1 parent 28765c5 commit 26e9329
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -4,3 +4,4 @@ build
components
/coverage
npm-debug.log
.idea
10 changes: 7 additions & 3 deletions lib/socket.js
Expand Up @@ -94,9 +94,12 @@ function Socket (uri, opts) {
this.rejectUnauthorized = opts.rejectUnauthorized === undefined ? true : opts.rejectUnauthorized;
this.forceNode = !!opts.forceNode;

// other options for Node.js client
// detect ReactNative environment
this.isReactNative = (typeof navigator !== 'undefined' && typeof navigator.product === 'string' && navigator.product.toLowerCase() === 'reactnative');

// other options for Node.js or ReactNative client
var freeGlobal = typeof global === 'object' && global;
if (freeGlobal.global === freeGlobal) {
if (freeGlobal.global === freeGlobal || this.isReactNative) {
if (opts.extraHeaders && Object.keys(opts.extraHeaders).length > 0) {
this.extraHeaders = opts.extraHeaders;
}
Expand Down Expand Up @@ -196,7 +199,8 @@ Socket.prototype.createTransport = function (name) {
forceNode: options.forceNode || this.forceNode,
localAddress: options.localAddress || this.localAddress,
requestTimeout: options.requestTimeout || this.requestTimeout,
protocols: options.protocols || void (0)
protocols: options.protocols || void (0),
isReactNative: this.isReactNative
});

return transport;
Expand Down
3 changes: 3 additions & 0 deletions lib/transport.js
Expand Up @@ -41,6 +41,9 @@ function Transport (opts) {
this.rejectUnauthorized = opts.rejectUnauthorized;
this.forceNode = opts.forceNode;

// results of ReactNative environment detection
this.isReactNative = opts.isReactNative;

// other options for Node.js client
this.extraHeaders = opts.extraHeaders;
this.localAddress = opts.localAddress;
Expand Down
2 changes: 1 addition & 1 deletion lib/transports/websocket.js
Expand Up @@ -109,7 +109,7 @@ WS.prototype.doOpen = function () {
}

try {
this.ws = this.usingBrowserWebSocket ? (protocols ? new WebSocket(uri, protocols) : new WebSocket(uri)) : new WebSocket(uri, protocols, opts);
this.ws = this.usingBrowserWebSocket && !this.isReactNative ? (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 26e9329

Please sign in to comment.