Skip to content

Commit

Permalink
improve reconnection
Browse files Browse the repository at this point in the history
  • Loading branch information
barisyild committed Apr 1, 2024
1 parent 75e97b4 commit 8ccbef3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/com/smartfoxserver/v2/bitswarm/BitSwarmClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ class BitSwarmClient extends EventDispatcher
_wsClient.close();
}

_connected = false;
executeDisconnection(null);
}

Expand Down
41 changes: 27 additions & 14 deletions src/com/smartfoxserver/v2/bitswarm/wsocket/WSClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 8ccbef3

Please sign in to comment.