Skip to content

Commit

Permalink
Merge pull request #187 from pusher/dont_reconnect_on_40xx
Browse files Browse the repository at this point in the history
Don't reconnect when codes in 40xx range are received
  • Loading branch information
kn100 authored Sep 27, 2018
2 parents a071cad + 3ca47d2 commit 1894ed2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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){

Expand Down Expand Up @@ -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 >= 4100;
}

private void cancelTimeoutsAndTransitonToDisconnected() {
activityTimer.cancelTimeouts();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down

0 comments on commit 1894ed2

Please sign in to comment.