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

Use ConcurrentHashMap for handling concurrent Android websockets, and… #17884

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -48,8 +49,8 @@ public interface ContentHandler {
void onMessage(ByteString byteString, WritableMap params);
}

private final Map<Integer, WebSocket> mWebSocketConnections = new HashMap<>();
private final Map<Integer, ContentHandler> mContentHandlers = new HashMap<>();
private final Map<Integer, WebSocket> mWebSocketConnections = new ConcurrentHashMap<>();
private final Map<Integer, ContentHandler> mContentHandlers = new ConcurrentHashMap<>();

private ReactContext mReactContext;
private ForwardingCookieHandler mCookieHandler;
Expand Down Expand Up @@ -224,8 +225,19 @@ public void close(int code, String reason, int id) {
public void send(String message, int id) {
WebSocket client = mWebSocketConnections.get(id);
if (client == null) {
// This is a programmer error
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
// This is a programmer error -- display development warning
WritableMap params = Arguments.createMap();
params.putInt("id", id);
params.putString("message", "client is null");
sendEvent("websocketFailed", params);
params = Arguments.createMap();
params.putInt("id", id);
params.putInt("code", 0);
params.putString("reason", "client is null");
sendEvent("websocketClosed", params);
mWebSocketConnections.remove(id);
mContentHandlers.remove(id);
return;
}
try {
client.send(message);
Expand All @@ -238,8 +250,19 @@ public void send(String message, int id) {
public void sendBinary(String base64String, int id) {
WebSocket client = mWebSocketConnections.get(id);
if (client == null) {
// This is a programmer error
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
// This is a programmer error -- display development warning
WritableMap params = Arguments.createMap();
params.putInt("id", id);
params.putString("message", "client is null");
sendEvent("websocketFailed", params);
params = Arguments.createMap();
params.putInt("id", id);
params.putInt("code", 0);
params.putString("reason", "client is null");
sendEvent("websocketClosed", params);
mWebSocketConnections.remove(id);
mContentHandlers.remove(id);
return;
}
try {
client.send(ByteString.decodeBase64(base64String));
Expand All @@ -251,8 +274,19 @@ public void sendBinary(String base64String, int id) {
public void sendBinary(ByteString byteString, int id) {
WebSocket client = mWebSocketConnections.get(id);
if (client == null) {
// This is a programmer error
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
// This is a programmer error -- display development warning
WritableMap params = Arguments.createMap();
params.putInt("id", id);
params.putString("message", "client is null");
sendEvent("websocketFailed", params);
params = Arguments.createMap();
params.putInt("id", id);
params.putInt("code", 0);
params.putString("reason", "client is null");
sendEvent("websocketClosed", params);
mWebSocketConnections.remove(id);
mContentHandlers.remove(id);
return;
}
try {
client.send(byteString);
Expand All @@ -265,8 +299,19 @@ public void sendBinary(ByteString byteString, int id) {
public void ping(int id) {
WebSocket client = mWebSocketConnections.get(id);
if (client == null) {
// This is a programmer error
throw new RuntimeException("Cannot send a message. Unknown WebSocket id " + id);
// This is a programmer error -- display development warning
WritableMap params = Arguments.createMap();
params.putInt("id", id);
params.putString("message", "client is null");
sendEvent("websocketFailed", params);
params = Arguments.createMap();
params.putInt("id", id);
params.putInt("code", 0);
params.putString("reason", "client is null");
sendEvent("websocketClosed", params);
mWebSocketConnections.remove(id);
mContentHandlers.remove(id);
return;
}
try {
client.send(ByteString.EMPTY);
Expand Down