Skip to content

Commit

Permalink
improve behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
barisyild committed Apr 2, 2024
1 parent 8ccbef3 commit ac62646
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/com/smartfoxserver/v2/bitswarm/wsocket/WSClient.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ class WSClient extends EventDispatcher
throw "WebSocket session is already connected";
}
_useSSL = useSSL;

if(ws != null)
{
ws.onopen = function () {};
ws.onmessageBytes = function(bytes:Bytes) {};
ws.onclose = function(?e:Dynamic) {};
ws.onerror = function(error:String) {};
}

ws = WebSocket.create(protocol + url + ":" + port +"/BlueBox/websocket", null, _debug);
ws.onopen = function () {
_connected = true;
Expand All @@ -64,7 +73,8 @@ class WSClient extends EventDispatcher
}));
};
ws.onclose = function(?e:Dynamic) {
handleConnectionLost();
_connected = false;
dispatchEvent(new WSEvent(WSEvent.CLOSED, {}));
};
ws.onerror = function(error:String) {
handleIOError(error);
Expand Down Expand Up @@ -110,20 +120,9 @@ class WSClient extends EventDispatcher

public function close() : Void
{
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, { }));
}
ws.onclose = function(?e:Dynamic) {};
_connected = false;
ws.close();
}

private function handleIOError(error:String):Void
Expand Down

0 comments on commit ac62646

Please sign in to comment.