From 0bdc7c34cfebfbd570b2416c5feeffc21ff59e27 Mon Sep 17 00:00:00 2001 From: kn100 Date: Tue, 28 Aug 2018 16:53:16 +0100 Subject: [PATCH 1/2] don't reconnect on codes in 40xx range --- .../websocket/WebSocketConnection.java | 10 ++++++ .../websocket/WebSocketConnectionTest.java | 32 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java index 199c249f..5fb57674 100644 --- a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java +++ b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java @@ -271,6 +271,10 @@ public void onClose(final int code, final String reason, final boolean remote) { return; } + if(!shouldReconnect(code)) { + updateState(ConnectionState.DISCONNECTING); + } + //Reconnection logic if(state == ConnectionState.CONNECTED || state == ConnectionState.CONNECTING){ @@ -303,6 +307,12 @@ public void run() { }, reconnectInterval, TimeUnit.SECONDS); } + // Received error codes 4000 >= 4099 indicate we shouldn't attempt reconnection + // https://pusher.com/docs/pusher_protocol#error-codes + private boolean shouldReconnect(int code) { + return !(code >= 4000 && code <= 4099); + } + private void cancelTimeoutsAndTransitonToDisconnected() { activityTimer.cancelTimeouts(); diff --git a/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java b/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java index ca7869fb..a3943a2c 100644 --- a/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java +++ b/src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java @@ -316,7 +316,37 @@ public void stateIsReconnectingAfterOnCloseWithoutTheUserDisconnecting() throws connection.connect(); connection.onMessage(CONN_ESTABLISHED_EVENT); - connection.onClose(500, "reason", true); + connection.onClose(3999, "reason", true); + + assertEquals(ConnectionState.RECONNECTING, connection.getState()); + } + + @Test + public void stateIsDisconnectedAfterOnCloseWithoutTheUserDisconnectingCode4000() throws InterruptedException, SSLException { + connection.connect(); + connection.onMessage(CONN_ESTABLISHED_EVENT); + + connection.onClose(4000, "reason", true); + + assertEquals(ConnectionState.DISCONNECTED, connection.getState()); + } + + @Test + public void stateIsDisconnectedAfterOnCloseWithoutTheUserDisconnectingCode4099() throws InterruptedException, SSLException { + connection.connect(); + connection.onMessage(CONN_ESTABLISHED_EVENT); + + connection.onClose(4099, "reason", true); + + assertEquals(ConnectionState.DISCONNECTED, connection.getState()); + } + + @Test + public void stateIsDisconnectedAfterOnCloseWithoutTheUserDisconnectingCode4100() throws InterruptedException, SSLException { + connection.connect(); + connection.onMessage(CONN_ESTABLISHED_EVENT); + + connection.onClose(4100, "reason", true); assertEquals(ConnectionState.RECONNECTING, connection.getState()); } From 3ca47d22fa3c871d3f655045303d6e0d5ea04b89 Mon Sep 17 00:00:00 2001 From: kn100 Date: Wed, 29 Aug 2018 13:31:42 +0100 Subject: [PATCH 2/2] refactor wrt will feedback --- .../client/connection/websocket/WebSocketConnection.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java index 5fb57674..15d41315 100644 --- a/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java +++ b/src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java @@ -307,10 +307,10 @@ public void run() { }, reconnectInterval, TimeUnit.SECONDS); } - // Received error codes 4000 >= 4099 indicate we shouldn't attempt reconnection + // Received error codes 4000-4099 indicate we shouldn't attempt reconnection // https://pusher.com/docs/pusher_protocol#error-codes private boolean shouldReconnect(int code) { - return !(code >= 4000 && code <= 4099); + return code < 4000 || code >= 4100; } private void cancelTimeoutsAndTransitonToDisconnected() {