From 8ccbef3b3d428387c1b854bf4870df1c75bf34b1 Mon Sep 17 00:00:00 2001 From: win11 Date: Mon, 1 Apr 2024 22:28:22 +0300 Subject: [PATCH] improve reconnection --- .../v2/bitswarm/BitSwarmClient.hx | 1 + .../v2/bitswarm/wsocket/WSClient.hx | 41 ++++++++++++------- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/src/com/smartfoxserver/v2/bitswarm/BitSwarmClient.hx b/src/com/smartfoxserver/v2/bitswarm/BitSwarmClient.hx index e5d62fb..b2bbeb7 100644 --- a/src/com/smartfoxserver/v2/bitswarm/BitSwarmClient.hx +++ b/src/com/smartfoxserver/v2/bitswarm/BitSwarmClient.hx @@ -428,6 +428,7 @@ class BitSwarmClient extends EventDispatcher _wsClient.close(); } + _connected = false; executeDisconnection(null); } diff --git a/src/com/smartfoxserver/v2/bitswarm/wsocket/WSClient.hx b/src/com/smartfoxserver/v2/bitswarm/wsocket/WSClient.hx index d7e0829..6e0db9a 100644 --- a/src/com/smartfoxserver/v2/bitswarm/wsocket/WSClient.hx +++ b/src/com/smartfoxserver/v2/bitswarm/wsocket/WSClient.hx @@ -21,9 +21,9 @@ class WSClient extends EventDispatcher super(); } - private function get_connected() : Bool + private inline function get_connected() : Bool { - return ws != null && _connected; + return _connected; } private function get_isDebug() : Bool @@ -64,19 +64,10 @@ class WSClient extends EventDispatcher })); }; ws.onclose = function(?e:Dynamic) { - if(!_connected) - return; - _connected = false; - dispatchEvent(new WSEvent(WSEvent.CLOSED, { })); - //ws = null; + handleConnectionLost(); }; ws.onerror = function(error:String) { - _connected = false; - var wsEvt : WSEvent = new WSEvent(WSEvent.IO_ERROR, { - message : error - }); - dispatchEvent(wsEvt); - //ws = null; + handleIOError(error); }; #if openfl @@ -119,7 +110,29 @@ class WSClient extends EventDispatcher public function close() : Void { - ws.close(); + handleConnectionLost(false); + } + + private function handleConnectionLost(fireEvent:Bool=true):Void + { + if(_connected) + { + _connected = false; + ws.close(); + + // Fire event to Bitswarm client + if(fireEvent) + dispatchEvent(new WSEvent(WSEvent.CLOSED, { })); + } + } + + private function handleIOError(error:String):Void + { + _connected = false; + var wsEvt : WSEvent = new WSEvent(WSEvent.IO_ERROR, { + message : error + }); + dispatchEvent(wsEvt); } }