Copy link to clipboard
Copied
How can I disconnect a client from ServerSocket?
Example code:
protected var socket:ServerSocket = new ServerSocket();
[Bindable] // for keeping track of the clients
protected var clientSockets:ArrayCollection = new ArrayCollection();
protected var clientIP:Array = new Array();
protected function createServer():void
{
socket.bind(1234,"0.0.0.0");
socket.addEventListener(ServerSocketConnectEvent.CONNECT, clientConnectedHandler);
// start listening for connections socket.listen();
}
// adds the client to the list and adds the disconnect handler
protected function clientConnectedHandler(event:ServerSocketConnectEvent😞void
{
clientSockets.addItem(event.socket);
clientIP.push(event.socket.remoteAddress);
event.socket.addEventListener(Event.CLOSE,clientDisconnectedHandler);
}
protected function clientDisconnectedHandler(event:Event😞void
{
clientIP.splice(clientSockets.getItemIndex(event.target),1);
clientSockets.removeItemAt(clientSockets.getItemIndex(event.target));
event.target.removeEventListener(Event.CLOSE,clientDisconnectedHandler);
}
public function closeServer():void
{
if(socket.bound){
socket.close();
}
}
From my code here, I am able to detect and know when the client is disconnected. But how can I disconnect a client connection from ServerSocket?
Thank you.
Copy link to clipboard
Copied
Are you having an issue with the socket remaining open on the server despite the client disconnecting or are you asking how you can close a clients connection yourself?
If the former then each connected client is an instance of the Socket class so you call the close() method on the connection.
If the latter, much like what is said above, you simply run all the commands in clientDisconnectedHandler() in addition to the close() method on the Socket while removing that reference from your clientSockets ArrayCollection.
Copy link to clipboard
Copied
Thanks a lot for your reply.
Now I am able to close a client connection via this:
var index:Number = clientIP.indexOf(ip);
var serversocket:Socket = clientSockets[index];
serversocket.close();
But it didn't dispatch the connection close event which will run the clientDisconnectedHandler function. How can I get the event to dispatch when I close the connection?
Thank you.
Copy link to clipboard
Copied
As the docs say for the close() method:
The close event is dispatched only when the server closes the connection; it is not dispatched when you call the close() method.
So the function you run to close a connection should do all the similar work the event handler is doing as well. Besides, the event handler should be running the close() method and you wouldn't want to run the close() method twice on the same socket.
You can always centralize that into a function, e.g.:
function RemoveClient(sock:Socket):void
{
// code to remove socket from ArrayCollection and close()
}
The handler and you can either call this same function to centralize the code into one function.
Otherwise just handle those two contexts differently. If you manually close one, duplicate the code necessary to fully prune your array and properly close your Socket.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now