Skip to content

Commit 2f5c948

Browse files
fix(react-native): restrict the list of options for the WebSocket object
Only 'headers' and 'localAddress' options are supported by the WebSocket implementation in React Native. The following message was printed to the console: > Unrecognized WebSocket connection option(s) `agent`, `perMessageDeflate`, `pfx`, `key`, `passphrase`, `cert`, `ca`, `ciphers`, `rejectUnauthorized`. Did you mean to put these under `headers`? Reference: https://reactnative.dev/docs/network.html#websocket-support
1 parent 86d4e8d commit 2f5c948

File tree

2 files changed

+27
-20
lines changed

2 files changed

+27
-20
lines changed

lib/socket.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,6 @@ class Socket extends Emitter {
8181
this.opts.query = parseqs.decode(this.opts.query);
8282
}
8383

84-
// detect ReactNative environment
85-
this.opts.isReactNative =
86-
typeof navigator !== "undefined" &&
87-
typeof navigator.product === "string" &&
88-
navigator.product.toLowerCase() === "reactnative";
89-
9084
// set on handshake
9185
this.id = null;
9286
this.upgrades = null;

lib/transports/websocket.js

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ const { WebSocket, usingBrowserWebSocket } = require("./websocket-constructor");
77

88
const debug = require("debug")("engine.io-client:websocket");
99

10+
// detect ReactNative environment
11+
const isReactNative =
12+
typeof navigator !== "undefined" &&
13+
typeof navigator.product === "string" &&
14+
navigator.product.toLowerCase() === "reactnative";
15+
1016
class WS extends Transport {
1117
/**
1218
* WebSocket transport constructor.
@@ -47,26 +53,33 @@ class WS extends Transport {
4753

4854
const uri = this.uri();
4955
const protocols = this.opts.protocols;
50-
const opts = pick(
51-
this.opts,
52-
"agent",
53-
"perMessageDeflate",
54-
"pfx",
55-
"key",
56-
"passphrase",
57-
"cert",
58-
"ca",
59-
"ciphers",
60-
"rejectUnauthorized",
61-
"localAddress"
62-
);
56+
57+
let opts;
58+
if (isReactNative) {
59+
opts = pick(this.opts, "localAddress");
60+
} else {
61+
opts = pick(
62+
this.opts,
63+
"agent",
64+
"perMessageDeflate",
65+
"pfx",
66+
"key",
67+
"passphrase",
68+
"cert",
69+
"ca",
70+
"ciphers",
71+
"rejectUnauthorized",
72+
"localAddress"
73+
);
74+
}
75+
6376
if (this.opts.extraHeaders) {
6477
opts.headers = this.opts.extraHeaders;
6578
}
6679

6780
try {
6881
this.ws =
69-
usingBrowserWebSocket && !this.opts.isReactNative
82+
usingBrowserWebSocket && !isReactNative
7083
? protocols
7184
? new WebSocket(uri, protocols)
7285
: new WebSocket(uri)

0 commit comments

Comments
 (0)