Skip to content

Commit fb80688

Browse files
committed
[WebSocket][Android] Support WebSocket sub-protocols on Android fixes #5810
1 parent a29d67e commit fb80688

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
@@ -64,7 +64,6 @@ public String getName() {
6464

6565
@ReactMethod
6666
public void connect(final String url, @Nullable final ReadableArray protocols, @Nullable final ReadableMap options, final int id) {
67-
// ignoring protocols, since OKHttp overrides them.
6867
OkHttpClient client = new OkHttpClient();
6968

7069
client.setConnectTimeout(10, TimeUnit.SECONDS);
@@ -86,6 +85,21 @@ public void connect(final String url, @Nullable final ReadableArray protocols, @
8685
}
8786
}
8887

88+
if (protocols != null && protocols.size() > 0) {
89+
StringBuilder protocolsValue = new StringBuilder("");
90+
for (int i = 0; i < protocols.size(); i++) {
91+
String v = protocols.getString(i).trim();
92+
if (!v.isEmpty() && !v.contains(",")) {
93+
protocolsValue.append(v);
94+
protocolsValue.append(",");
95+
}
96+
}
97+
if (protocolsValue.length() > 0) {
98+
protocolsValue.replace(protocolsValue.length() - 1, protocolsValue.length(), "");
99+
builder.addHeader("Sec-WebSocket-Protocol", protocolsValue.toString());
100+
}
101+
}
102+
89103
WebSocketCall.create(client, builder.build()).enqueue(new WebSocketListener() {
90104

91105
@Override

0 commit comments

Comments
 (0)