Skip to content

Commit 286e633

Browse files
committed
[WebSocket][Android] Support WebSocket sub-protocols on Android fixes facebook#5810
1 parent 9adef91 commit 286e633

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

ReactAndroid/src/main/java/com/facebook/react/modules/websocket/WebSocketModule.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ public String getName() {
6666

6767
@ReactMethod
6868
public void connect(final String url, @Nullable final ReadableArray protocols, @Nullable final ReadableMap headers, final int id) {
69-
// ignoring protocols, since OKHttp overrides them.
7069
OkHttpClient client = new OkHttpClient();
7170

7271
client.setConnectTimeout(10, TimeUnit.SECONDS);
@@ -99,6 +98,21 @@ public void connect(final String url, @Nullable final ReadableArray protocols, @
9998
builder.addHeader("origin", setDefaultOrigin(url));
10099
}
101100

101+
if (protocols != null && protocols.size() > 0) {
102+
StringBuilder protocolsValue = new StringBuilder("");
103+
for (int i = 0; i < protocols.size(); i++) {
104+
String v = protocols.getString(i).trim();
105+
if (!v.isEmpty() && !v.contains(",")) {
106+
protocolsValue.append(v);
107+
protocolsValue.append(",");
108+
}
109+
}
110+
if (protocolsValue.length() > 0) {
111+
protocolsValue.replace(protocolsValue.length() - 1, protocolsValue.length(), "");
112+
builder.addHeader("Sec-WebSocket-Protocol", protocolsValue.toString());
113+
}
114+
}
115+
102116
WebSocketCall.create(client, builder.build()).enqueue(new WebSocketListener() {
103117

104118
@Override

0 commit comments

Comments
 (0)