@@ -131,8 +131,9 @@ public boolean isConnected() {
131131 * however if you decided otherwise, you may connect manually using this method.
132132 */
133133 public synchronized CompletableFuture <Void > connect () {
134+ val currentStatus = this .status ;
134135 Preconditions .checkState (
135- this . status == ProviderStatus .DISCONNECTED || this . status == ProviderStatus .CONNECTING ,
136+ currentStatus == ProviderStatus .DISCONNECTED || currentStatus == ProviderStatus .CONNECTING ,
136137 "WebSocket is already connected" );
137138
138139 var inProgress = this .whenConnected ;
@@ -178,27 +179,24 @@ public synchronized CompletableFuture<Void> connect() {
178179 */
179180 @ Override
180181 public synchronized CompletableFuture <Void > disconnect () {
182+ val currentStatus = this .status ;
183+ var inProgress = this .whenDisconnected ;
184+
181185 Preconditions .checkState (
182- this .status == ProviderStatus .CONNECTED || this .status == ProviderStatus .DISCONNECTING ,
186+ currentStatus == ProviderStatus .CONNECTED ||
187+ (currentStatus == ProviderStatus .DISCONNECTING && inProgress != null ),
183188 "WebSocket is not connected" );
184189
185- var inProgress = this .whenDisconnected ;
186190 if (inProgress != null ) {
187191 return inProgress ;
188192 }
189193
190- this .status = ProviderStatus .DISCONNECTING ;
191-
192194 val whenDisconnected = new CompletableFuture <Void >();
193195 this .whenDisconnected = whenDisconnected ;
194196
195197 // switch off autoConnect, we are in manual mode now
196198 this .autoConnectMs = 0 ;
197-
198- this .eventEmitter .once (ProviderInterfaceEmitted .DISCONNECTED , _x -> {
199- whenDisconnected .complete (null );
200- this .whenDisconnected = null ;
201- });
199+ this .status = ProviderStatus .DISCONNECTING ;
202200
203201 val ws = this .webSocket ;
204202 if (ws != null ) {
@@ -343,7 +341,12 @@ private void emit(ProviderInterfaceEmitted type, Object... args) {
343341 this .eventEmitter .emit (type , args );
344342 }
345343
346- private void onSocketClose (int code , String reason ) {
344+ private synchronized void onSocketClose (int code , String reason ) {
345+ val currentStatus = this .status ;
346+ if (currentStatus == ProviderStatus .CONNECTED || currentStatus == ProviderStatus .CONNECTING ) {
347+ this .status = ProviderStatus .DISCONNECTING ;
348+ }
349+
347350 if (Strings .isNullOrEmpty (reason )) {
348351 reason = ErrorCodes .getWSErrorString (code );
349352 }
@@ -368,6 +371,11 @@ private void onSocketClose(int code, String reason) {
368371 this .webSocket = null ;
369372 this .status = ProviderStatus .DISCONNECTED ;
370373 this .emit (ProviderInterfaceEmitted .DISCONNECTED );
374+ val whenDisconnected = this .whenDisconnected ;
375+ if (whenDisconnected != null ) {
376+ whenDisconnected .complete (null );
377+ this .whenDisconnected = null ;
378+ }
371379
372380 if (this .autoConnectMs > 0 ) {
373381 log .info ("Trying to reconnect to {}" , this .endpoint );
@@ -455,7 +463,7 @@ private void onSocketMessageSubscribe(JsonRpcResponseSubscription response) {
455463 }
456464 }
457465
458- public void onSocketOpen () {
466+ public synchronized void onSocketOpen () {
459467 log .info ("Connected to: {}" , this .webSocket .getURI ());
460468
461469 this .status = ProviderStatus .CONNECTED ;
@@ -466,7 +474,8 @@ public void onSocketOpen() {
466474 @ Override
467475 public void close () {
468476 try {
469- if (this .status == ProviderStatus .CONNECTED || this .status == ProviderStatus .DISCONNECTING ) {
477+ val currentStatus = this .status ;
478+ if (currentStatus == ProviderStatus .CONNECTED || currentStatus == ProviderStatus .DISCONNECTING ) {
470479 this .disconnect ();
471480 }
472481 } catch (Exception ex ) {
0 commit comments