node.js - Nodejs check to see if client is still connected -


i using v0.10.37. logic set detection code not correct.

if (typeof(socket.to(key).connected[key]) ==='undefined')

is i'm using detect, tried typeof(socket.to(key).connected[key].connected threw undefined error (reason why changed code above). need code work clients array when adding new clientid client removes invalid clientids notifications can pushed smoothly client.

var http = require('http'); var io = require('socket.io'); server = http.createserver(function(req, res){}); server.listen(8082); var socket = io.listen(server); var clients = {}; socket.on('connection', function(client) {     client.on('setuserid',function(userid)     {         console.log("add");         if(typeof(clients[userid[0]])==='undefined')          clients[userid[0]] = {};         clients[userid[0]][userid[1]] = userid[1];         console.log(clients);            for(var key in clients[userid[0]])          {             if (typeof(socket.to(key).connected[key]) ==='undefined')              delete clients[userid[0]][userid[1]];         }        });     client.on('removeuserid',function(userid)     {         console.log("remove");         delete clients[userid[0]][userid[1]];         counter = 0;         for(var key in clients[userid[0]])          counter++;         if (counter == 0)         delete clients[userid[0]];           console.log(clients);     });      client.on('message', function(msg)     {         if (msg.indexof("notifications") != -1)         {             userid = msg.replace("notifications","");             for(var key in clients[userid])              {                 sessionid = key;                 console.log("notification sent" + msg);                 socket.to(sessionid).emit('message',msg);             }                    }         else if (msg.indexof("message") != -1)         {             userid = msg.replace("message","");             for(var key in clients[userid])              {                 sessionid = key;                 console.log("private message sent" + msg);                 socket.to(sessionid).emit('message',msg);             }         }                else         {             ids = msg.split(",");             (i = 0; < ids.length;i++)             {                 userid = ids[i];                 for(var key in clients[userid])                  {                     sessionid = key;                     displaymessage = "notifications" + msg;                     console.log("notification sent" + displaymessage);                     socket.to(sessionid).emit('message',displaymessage);                 }             }         }     }) });  

i fixed myself, issue deleting wrong index.

delete clients[userid[0]][key]

did trick.


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -