Question
ServerSocket disconnect client
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.