javascript - How to remove websocket connection onClose properly -
please take src-code:
var clients = {}; // map<string, connection> clients var clientsreverse = {}; // map<connection, string> clientsreverse websocketserver.on('connection', function(newclient) { // newclient connection object newclient.on('message', function(message) { var data = json.parse(message); if (clients[data.name] === undefined) { clients[data.name] = newclient; clientsreverse[newclient] = data.name; } // ... }); newclient.on('close', function(){ log(clientsreverse[newclient]); // prints same string // remove closed connection var key = clientsreverse[newclient]; delete clientsreverse[newclient]; delete clients[key]; }); });
i have remove connection if client gone away. can see i'm using 2 arrays this, unfortunately not work. if try disconnect 3 random clients, log print same name...
any idea i'm doing wrong?
neither clients
nor clientsreverse
array. objects. objects act kinda hashmaps in javascript, can access properties strings. example:
var = { b: 5 }; console.log(a['b']); //5
if try make index out of object (newclient
object), tostring
first. result of newclient.tostring()
[object object]
, , every object use, doesn't matter it's different object. that's why though use different newclient
objects, resolves same key.
i'm not sure want achieve here. can try using arrays ([]
), can have numerical indexes. can try using kind of unique property of client, id or name perhaps?
Comments
Post a Comment