Can't handle this disconnect event, don't know why socket is not sent to the client / client doesn't respond!
Server
io.sockets.on('connection', function (socket) { socket.on('NewPlayer', function(data1) { online = online + 1; console.log('Online players : ' + online); console.log('New player connected : ' + data1); Players[data1] = data1; console.log(Players); }); socket.on('DelPlayer', function(data) { delete Players[data]; console.log(Players); console.log('Adios' + data); }); socket.on('disconnect', function () { socket.emit('disconnected'); online = online - 1; }); });
Client
var socket = io.connect('http://localhost'); socket.on('connect', function () { person_name = prompt("Welcome. Please enter your name"); socket.emit('NewPlayer', person_name); socket.on('disconnected', function() { socket.emit('DelPlayer', person_name); }); });
As you can see when a client disconnects the Array object[person_name] should be deleted, but it's not.
click(function(){ socket. disconnect(); }); It disconnects alright.
var socket = io. connect('http://localhost:3000/test'); socket. on('disconnect', function () { console. log('disconnect client event....'); }); socket.
So, you can configure both the server and client side timeout settings by setting the following timeout properties on the engine.io component inside of socket.io.
Ok, instead of identifying players by name track with sockets through which they have connected. You can have a implementation like
var allClients = []; io.sockets.on('connection', function(socket) { allClients.push(socket); socket.on('disconnect', function() { console.log('Got disconnect!'); var i = allClients.indexOf(socket); allClients.splice(i, 1); }); });
Hope this will help you to think in another way
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With