How to handle client disconnect in Socket Programming?
By : Insomnia94
Date : March 29 2020, 07:55 AM
like below fixes the issue Your application crashes, because you throw an exception in the catch block of your method. If you don't want your application to crash, you need to remove the throw new Exception(ex.Message); line from the catch block. Replace it with code that handles the error and gracefully restores your application to a safe state. From reading your code, this should be done by removing the clientSocket from connectedClients code :
private void OnReceive(IAsyncResult result)
{
try
{
Socket clientSocket = (Socket)result.AsyncState;
clientSocket.EndReceive(result);
command = responseMessage = string.Empty;
command = ByteToString(receviedData);
receviedData = new byte[30];
if (command=="Connect")
{
ClientInfo clientInfo = new ClientInfo() {
socket = clientSocket,
IP = clientSocket.RemoteEndPoint.ToString(),
};
connectedClients.Add(clientInfo);
responseMessage = "Connection established...";
}
else if (command=="Disconnect")
{
removeClientInfo(clientSocket);
clientSocket.Close();
}
else
{
responseMessage = "Error";
}
byte[] responseStatus = StringToByte(responseMessage);
for (int i = 0; i < connectedClients.Count; i++)
{
if (connectedClients[i].socket==clientSocket)
{
connectedClients[i].socket.BeginSend(responseStatus, 0, responseStatus.Length,SocketFlags.None, new AsyncCallback(OnSend), connectedClients[i].socket);
break;
}
}
}
catch(Exception ex)
{
// add error handling and gracefully recover
// caution: The way done here, might work, but it smells :-/
removeClientInfo((Socket)result.AsyncState);
((Socket)result.AsyncState).Close();
}
}
/// removes the client info from the connectedClients enumerable
private void removeClientInfo(Socket socket)
{
for (int i = 0; i < connectedClients.Count; i++)
{
if (connectedClients[i].socket == socket)
{
connectedClients.RemoveAt(i);
break;
}
}
}
|
NodeJS + Cluster + Socket.io + Redis - iOS not leaving rooms after disconnect
By : Борис
Date : March 29 2020, 07:55 AM
will be helpful for those in need So I discovered that by using my own methods of reference counting, i can bypass needing socket.io to be accurate. Since i'm already using Redis to back my sockets through pubsub, i just create another client connection to redis and setex the socket.id's on an expiration. When my sockets unsubscribe or disconnect, i delete all the keys in Redis associated with the socket.id. This way, i have an instant snapshot of whos on, and if the key for some reason didn't get deleted on disconnect/unsub, it will expire based on what i set for the setex call. I'll try and remember to come back in two days to mark my own answer as correct.
|
SocketIO Disconnect Leaving Socket Listening
By : Waffelei
Date : March 29 2020, 07:55 AM
this will help The problem was caused by the location of my redis.on 'message' listener. I was listenting for that on each and every socket that I created. In reality, I only needed to listen for it in a global sense and then message any rooms. This looks like this: code :
io.on 'connection', (socket) ->
log.info "Connection established. [Socket: #{socket.id}]"
socket.on 'join_room', (roomName) ->
log.info "Join room '#{roomName}'. [Socket: #{socket.id}]"
socket.join roomName
socket.on 'disconnect', ->
log.info "Connection destroyed. [Socket #{socket.id}]"
redis.on 'message', (redisChannel, message) ->
message = JSON.parse message
log.info "Room: #{message.room} - Event: #{message.event} - Message: #{message.message}"
io.sockets.in(message.room).emit(message.event, message.message)
|
Java android client socket not getting response until after socket disconnect
By : Void Ping
Date : March 29 2020, 07:55 AM
I wish did fix the issue. Try adding a \n to the end of the message. Also do not set 'NoDelay` to false. You are doing an asynchronous send.
|
Socket.io server not receiving 'disconnect' event when client calls disconnect
By : jackchan7808
Date : March 29 2020, 07:55 AM
To fix the issue you can do I think this is a typical synchronicity problem (read explanations in code comments). code :
// This code will try to connect the server -
// but the connection process is asynchronous
const mj = ioClient(`https://localhost:${socketPort}`);
// Now you subscribe to the disconnect event which is fine
mj.on('disconnect', () => {
console.log('mj disconnect');
});
// And now you try to disconnect immediately which is done synchronously
// but how do you want to disconnect since you are still not connected ...
mj.disconnect();
const mj = ioClient(`https://localhost:${socketPort}`);
mj.on('connect', () => {
// Subscribe to disconnect event once you know you are connected
mj.on('disconnect', () => {
console.log('mj disconnect');
});
// Now disconnect once you are connected
mj.disconnect();
});
|