Skip to content

Commit 1894ed2

Browse files
authored
Merge pull request #187 from pusher/dont_reconnect_on_40xx
Don't reconnect when codes in 40xx range are received
2 parents a071cad + 3ca47d2 commit 1894ed2

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/main/java/com/pusher/client/connection/websocket/WebSocketConnection.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ public void onClose(final int code, final String reason, final boolean remote) {
271271
return;
272272
}
273273

274+
if(!shouldReconnect(code)) {
275+
updateState(ConnectionState.DISCONNECTING);
276+
}
277+
274278
//Reconnection logic
275279
if(state == ConnectionState.CONNECTED || state == ConnectionState.CONNECTING){
276280

@@ -303,6 +307,12 @@ public void run() {
303307
}, reconnectInterval, TimeUnit.SECONDS);
304308
}
305309

310+
// Received error codes 4000-4099 indicate we shouldn't attempt reconnection
311+
// https://pusher.com/docs/pusher_protocol#error-codes
312+
private boolean shouldReconnect(int code) {
313+
return code < 4000 || code >= 4100;
314+
}
315+
306316
private void cancelTimeoutsAndTransitonToDisconnected() {
307317
activityTimer.cancelTimeouts();
308318

src/test/java/com/pusher/client/connection/websocket/WebSocketConnectionTest.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,37 @@ public void stateIsReconnectingAfterOnCloseWithoutTheUserDisconnecting() throws
316316
connection.connect();
317317
connection.onMessage(CONN_ESTABLISHED_EVENT);
318318

319-
connection.onClose(500, "reason", true);
319+
connection.onClose(3999, "reason", true);
320+
321+
assertEquals(ConnectionState.RECONNECTING, connection.getState());
322+
}
323+
324+
@Test
325+
public void stateIsDisconnectedAfterOnCloseWithoutTheUserDisconnectingCode4000() throws InterruptedException, SSLException {
326+
connection.connect();
327+
connection.onMessage(CONN_ESTABLISHED_EVENT);
328+
329+
connection.onClose(4000, "reason", true);
330+
331+
assertEquals(ConnectionState.DISCONNECTED, connection.getState());
332+
}
333+
334+
@Test
335+
public void stateIsDisconnectedAfterOnCloseWithoutTheUserDisconnectingCode4099() throws InterruptedException, SSLException {
336+
connection.connect();
337+
connection.onMessage(CONN_ESTABLISHED_EVENT);
338+
339+
connection.onClose(4099, "reason", true);
340+
341+
assertEquals(ConnectionState.DISCONNECTED, connection.getState());
342+
}
343+
344+
@Test
345+
public void stateIsDisconnectedAfterOnCloseWithoutTheUserDisconnectingCode4100() throws InterruptedException, SSLException {
346+
connection.connect();
347+
connection.onMessage(CONN_ESTABLISHED_EVENT);
348+
349+
connection.onClose(4100, "reason", true);
320350

321351
assertEquals(ConnectionState.RECONNECTING, connection.getState());
322352
}

0 commit comments

Comments
 (0)