I have a very simple socket.io connection. My server does the following:
io.sockets.on("connection", function (socket) {
console.log("CONNECTED ON SERVER");
socket.emit("connected", "CONNECTED!");
});
Indeed, the client gets the "CONNECTED!" message.
I need to be able to detect when a client disconnects and take some action. I've tried:
io.sockets.on("disconnect", function (socket) {
console.log("DISCONNECTED!");
});
...but this does not log "DISCONNECTED!" on the server. However, CONNECTED ON SERVER from above does get logged.
I know that the socket can detect the disconnect because once I close the browser window, the server logs out:
info - transport end (socket end)
debug - set close timeout for client
debug - cleared close timeout for client
debug - cleared heartbeat interval for client
debug - discarding transport
This means that socket.io is able to detect that the client connection closed. How can I detect that it was closed?