Skip to content

Commit a0ffae0

Browse files
authored
Handling some WebSocket connection errors gracefully (#157)
* Handling some WebSocket connection errors gracefully * Logging only channel close events in debug mode * Removing redundant null check
1 parent aca5fcc commit a0ffae0

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/main/java/com/google/firebase/database/connection/NettyWebSocketClient.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import io.netty.handler.ssl.SslContext;
3434
import io.netty.handler.ssl.SslContextBuilder;
3535

36+
import java.io.EOFException;
3637
import java.net.URI;
3738
import java.security.KeyStore;
3839
import java.util.concurrent.ExecutorService;
@@ -130,8 +131,12 @@ public void close() {
130131

131132
@Override
132133
public void send(String msg) {
133-
checkState(channel != null && channel.isActive(), "channel not connected for sending");
134-
channel.writeAndFlush(new TextWebSocketFrame(msg));
134+
checkState(channel != null, "Channel not initialized");
135+
if (!channel.isActive()) {
136+
eventHandler.onError(new EOFException("WebSocket channel became inactive"));
137+
} else {
138+
channel.writeAndFlush(new TextWebSocketFrame(msg));
139+
}
135140
}
136141

137142
/**

src/main/java/com/google/firebase/database/connection/WebsocketConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,8 +324,8 @@ public void run() {
324324

325325
@Override
326326
public void onError(final Throwable e) {
327-
if (e.getCause() != null && e.getCause() instanceof EOFException) {
328-
logger.error("WebSocket reached EOF", e);
327+
if (e instanceof EOFException || e.getCause() instanceof EOFException) {
328+
logger.debug("WebSocket reached EOF", e);
329329
} else {
330330
logger.error("WebSocket error", e);
331331
}

0 commit comments

Comments
 (0)