@@ -185,9 +185,12 @@ void processDisconnect(int code, String reason, Boolean shouldReconnect) {
185185 this .reconnectTask .cancel (true );
186186 this .reconnectTask = null ;
187187 }
188+ boolean needEvent ;
188189 if (shouldReconnect ) {
190+ needEvent = previousState != ClientState .CONNECTING ;
189191 this .setState (ClientState .CONNECTING );
190192 } else {
193+ needEvent = previousState != ClientState .DISCONNECTED ;
191194 this .setState (ClientState .DISCONNECTED );
192195 }
193196
@@ -212,12 +215,14 @@ void processDisconnect(int code, String reason, Boolean shouldReconnect) {
212215 }
213216 }
214217
215- if (shouldReconnect ) {
216- ConnectingEvent event = new ConnectingEvent (code , reason );
217- this .listener .onConnecting (this , event );
218- } else {
219- DisconnectedEvent event = new DisconnectedEvent (code , reason );
220- this .listener .onDisconnected (this , event );
218+ if (needEvent ) {
219+ if (shouldReconnect ) {
220+ ConnectingEvent event = new ConnectingEvent (code , reason );
221+ this .listener .onConnecting (this , event );
222+ } else {
223+ DisconnectedEvent event = new DisconnectedEvent (code , reason );
224+ this .listener .onDisconnected (this , event );
225+ }
221226 }
222227
223228 this .ws .close (NORMAL_CLOSURE_STATUS , "" );
@@ -335,12 +340,15 @@ public void onFailure(WebSocket webSocket, Throwable t, Response response) {
335340 });
336341 }
337342
338- private void handleConnectionOpen () {
343+ private void handleConnectionOpen () throws Exception {
339344 if (this .getState () != ClientState .CONNECTING ) {
340345 return ;
341346 }
342347 if (this .refreshRequired || (this .token == null && this .opts .getTokenGetter () != null )) {
343348 ConnectionTokenEvent connectionTokenEvent = new ConnectionTokenEvent ();
349+ if (this .opts .getTokenGetter () == null ) {
350+ throw new Exception ("tokenGetter function should be provided in Client options to handle token refresh, see Options.setTokenGetter" );
351+ }
344352 this .opts .getTokenGetter ().getConnectionToken (connectionTokenEvent , (err , token ) -> this .executor .submit (() -> {
345353 if (Client .this .getState () != ClientState .CONNECTING ) {
346354 return ;
@@ -597,6 +605,7 @@ private void handleConnectReply(Protocol.Reply reply) {
597605 sub .resubscribeIfNecessary ();
598606 }
599607 }
608+
600609 for (Map .Entry <String , Protocol .SubscribeResult > entry : result .getSubsMap ().entrySet ()) {
601610 Protocol .SubscribeResult subResult = entry .getValue ();
602611 String channel = entry .getKey ();
@@ -656,6 +665,7 @@ private void handleConnectReply(Protocol.Reply reply) {
656665 }
657666 }
658667 }
668+
659669 this .connectCommands .clear ();
660670
661671 for (Map .Entry <Integer , Protocol .Command > entry : this .connectAsyncCommands .entrySet ()) {
0 commit comments