Fix IllegalStateException crash in WebSocketModule.java

Summary:- Motivation: The WebSocket implementation on Android crashes the app when an attempt is made to write on a web socket that was closed due to a spotty connection. We found this issue by using Pusher, which is built on WebSockets. The following stack trace reveals that the WebSocketModule doesn't catch the case of a closed connection, when a consumer attempts to write:
```sh
Fatal Exception: java.lang.IllegalStateException: closed
       at com.squareup.okhttp.internal.ws.RealWebSocket.sendMessage(RealWebSocket.java:109)
       at com.facebook.react.modules.websocket.WebSocketModule.send(WebSocketModule.java:176)
       at java.lang.reflect.Method.invoke(Method.java)
       at java.lang.reflect.Method.invoke(Method.java:372)
       at com.facebook.react.bridge.BaseJavaModule$JavaMethod.invoke(BaseJavaModule.java:249)
       at com.facebook.react.bridge.NativeModuleRegistry$ModuleDefinition.call(NativeModuleRegistry.java:158)
       at com.facebook.react.bridge.NativeModuleRegistry.call(NativeModuleReg
Closes https://github.com/facebook/react-native/pull/6301

Differential Revision: D3016099

fb-gh-sync-id: 838dd9d2e5e5b7a4e2242fa6de5658dfdaf24f55
shipit-source-id: 838dd9d2e5e5b7a4e2242fa6de5658dfdaf24f55
This commit is contained in:
Kevin Stumpf 2016-03-05 00:42:07 -08:00 committed by Facebook Github Bot 6
parent 970782d147
commit a6a89fe4e0
1 changed files with 2 additions and 1 deletions

View File

@ -10,6 +10,7 @@
package com.facebook.react.modules.websocket; package com.facebook.react.modules.websocket;
import java.io.IOException; import java.io.IOException;
import java.lang.IllegalStateException;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import com.facebook.common.logging.FLog; import com.facebook.common.logging.FLog;
@ -176,7 +177,7 @@ public class WebSocketModule extends ReactContextBaseJavaModule {
client.sendMessage( client.sendMessage(
WebSocket.PayloadType.TEXT, WebSocket.PayloadType.TEXT,
new Buffer().writeUtf8(message)); new Buffer().writeUtf8(message));
} catch (IOException e) { } catch (IOException | IllegalStateException e) {
notifyWebSocketFailed(id, e.getMessage()); notifyWebSocketFailed(id, e.getMessage());
} }
} }