node.js - Approach of sending private messages to specific clients in SocketIO and Android -


i have problem sending private messages using socketio on nodejs server , android on client side. messages not reach specific endpoint have assigned , not understand why is. know each time user connects socketio, assigned specific socket id save in object alongside socket object using client name identifier. so:

  // register client server, providing username socket.on('init', function(data) {   console.log("userid assigned socket: " + data.userid); // store reference socket id // data.userid identifier find respective client want send message to.   connectedsockets[data.userid] = { id: socket.id, usersocket : socket };  // store reference socket   console.log("new connected socketid: " + connectedsockets[data.userid].id + " socket object: " + connectedsockets[data.userid].usersocket); }); 

when sending private message, want retrieve object send respective person so:

// send message user  socket.on('privatemessage', function(data){    // send message user   console.log("receivingid : " + data.recipientid);   console.log("socketid used: " + connectedsockets[data.recipientid].id);    var usersocket = connectedsockets[data.recipientid].usersocket;   var socketid = connectedsockets[data.recipientid].id   var recipient = data.recipientid;    io.sockets.usersocket[socketid].emit(recipient, {message: data.message});  }); 

however, reason undefined error socket , id:

var usersocket = connectedsockets[data.recipientid].usersocket; typeerror: cannot read property 'usersocket' of undefined 

i connecting both clients , both id's saved in connectedsockets still getting undefined error.

any idea why is?

additionally, wondering if understanding right. when wanting send message specific client using socket object received when client connecting , socketid assigned client emit message command:

io.sockets.usersocket[socketid].emit(recipient, {message: data.message}); 

is right approach on using private messaging in socketio?


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -