Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect React-Native environment and use all websocket features #591

Merged
merged 1 commit into from
Nov 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ build
components
/coverage
npm-debug.log
.idea
10 changes: 7 additions & 3 deletions lib/socket.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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