mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-09 22:43:10 +08:00
Add ping to WebSocket
Summary:
Idle WebSocket connections get reset after a few minutes of inactivity. To prevent this, most WebSocket implementations offer sending special ping messages. This PR adds a method `sendPing()` to WebSocket. Ping payloads are not supported.
Manual testing can be done by adding `connection.on('ping', _ => console.log('Received ping'));` to a ws connection or using a packet sniffer while sending pings.
Closes https://github.com/facebook/react-native/pull/8505
Differential Revision: D3516260
Pulled By: dmmiller
fbshipit-source-id: cfebf5899188ae53254d5be6b666a9075e0eed89
This commit is contained in:
committed by
Facebook Github Bot 4
parent
f48babaa5e
commit
4ac4f86bf5
@@ -229,6 +229,21 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
|
||||
}
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
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);
|
||||
}
|
||||
try {
|
||||
Buffer buffer = new Buffer();
|
||||
client.sendPing(buffer);
|
||||
} catch (IOException | IllegalStateException e) {
|
||||
notifyWebSocketFailed(id, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyWebSocketFailed(int id, String message) {
|
||||
WritableMap params = Arguments.createMap();
|
||||
params.putInt("id", id);
|
||||
|
||||
Reference in New Issue
Block a user