How can I handle the Close event in Socket.io?

I am making a simple web based online game. The game uses Socket.io to interact with each other. but I ran into a problem.

I think about the following situation. I started the Socket.io server. one player makes a room, and another player enters the room. they played the game for a while .. but one player got so angry and closed the game’s tab.

in this situation, how can I get an event that one client was closed by a server-side browser?

according to googling, people say this: "use browser close event, for example onBeforeUnload"

but I know that the browser does not support the onBeforeUnload event. so I want a solution about checking client disconnect events on the server side.

in the Socket.io server console (nodeJS), when the client connection is closed, the console will say the following:

debugging - dropping a transport

My version of nodeJS is 0.4.10 and the version of Socket.io is 0.8.7. and both work on Linux.

Can anybody help?

abbreviation codes:

var io = require ( "socket.io" ).listen ( 3335 ); io.sockets.on ( "connection" , function ( socket ) { socket.on ( "req_create_room" , function ( roomId ) { var socketInstance = io .of ( "/" + roomId ) .on ( "connection" , function ( sock ) { sock.on ( "disconnect" , function () { // i want this socket data always displayed... // but first-connected-client doesn't fire this event .. console.log ( sock ); } }); }); }); 
+12
Jan 31 '12 at 10:11
source share
2 answers

Update: I created a blog post for this solution. Any feedback is appreciated!

I recommend using the "sync disconnect on unload" option for Socket IO. I had similar problems and it really helped me.

On the client:

 var socket = io.connect(<your_url>, { 'sync disconnect on unload': true }); 

No need to connect to unload events or before loading. Tried this in multiple browsers and it has worked fine so far.

+17
Feb 05 '13 at 3:42
source share

There's a disconnect event that fires whenever a socket.io connection dies (note that you need it because you can still have a wep page, but what if your internet connection dies?). Try the following:

 var io = require('socket.io').listen(80); io.sockets.on('connection', function (socket) { socket.on('disconnect', function () { io.sockets.emit('user disconnected'); }); }); 

on your server. Taken from the Socket.IO website .

// EDIT

So, I looked at your code and did some tests at home. I got the same results as you, and here are my explanations. You are trying to make Socket.IO very dynamic by dynamically making it listen to different URLs (which are added at runtime). But when the first connection occurs, the server does not listen on another URL. It seems that it is at this moment (when the connection is accepted) that the disconnect handler is installed for the first connection and does not update later (note that Socket.IO does not create new connections when you call io.connect many times on the client side). All in all, this seems like a mistake! Or maybe there is some kind of bizarre explanation of why this behavior should be the way it is, but I don’t know that.

Let me tell you something else. First of all, this dynamic creation of listeners doesn't seem to be a good way. In my opinion, you should do the following: store existing rooms and use one URL for all. Hold the room identifier, and when you select, for example, the message event from the client, add the room identifier to the data and process it with one message handler on the server. I think you get the point. Click the dynamic part on the data, not the URL. But I could be wrong, at least my opinion.

Another thing is that the code you wrote seems to be bad. Note that running .on('connection', handler) many times will make it fire many times. Handlers stack one on top of another; they do not replace each other. So here is how I could implement this:

 var io = require("socket.io").listen(app); var roomIds = []; function update_listeners(id) { io.of("/"+id).on("connection", function(socket) { console.log("I'm in room " + id); socket.on("disconnect", function(s) { console.log("Disconnected from " + roomId); }); }); } var test = io.sockets.on("connection", function(socket) { console.log("I'm in global connection handler"); socket.on("req_create_room", function(data) { if (roomIds.indexOf(data.roomId) == -1 ) { roomIds.push(data.roomId); update_listeners(data.roomId); } test.emit("room_created", {ok:true}); }); socket.on("disconnect", function(s) { console.log("Disconnected from global handler"); }); }); 

Keep in mind that the problem of creating connections before listeners are defined will still occur.

+6
Jan 31 '12 at 10:27
source share



All Articles