node.js - Issue with re-join same room after reconnect SocketIO -


i'm trying understand why following code doesn't work reason. want same client able rejoin same room, somehow when print list of clients in room after client reconnect, client shows up, rests not there???

client

var id=null, socket = null; var publickey = null; var first = math.random().tostring(36).substring(2,8); var last = math.random().tostring(36).substring(2,8); $('#bt1').click(function(){     socket = io();     socket.on('connect',function(){         addtext('connect server. first-last '+ first +' '+last);     });     socket.on('disconnect',function(data){                   addtext('disconnect server');      });     socket.on('verifyid_1',function(data){         id = data.id;         socket.emit('verifyid_2',{id:id,publickey:publickey})         addtext('clientid '+id);      });     socket.on('gamecreated',function(data){         publickey = data.publickey;         addtext('publickey '+publickey);     });     socket.on('playerrejoinroom',function(data){addtext(json.stringify(data))}); }); $('#bt2').click(function(){ socket.connect();}); $('#bt3').click(function(){ socket.disconnect();}); $('#bt4').click(function(){     publickey = $('#input').val(); addtext('publickey '+publickey+' entered');     socket.emit('joinroom',{publickey:publickey}); }); $('#bt5').click(function(){     addtext('create new game clientid '+id);     socket.emit('creategame',{id:id,numberofplayers:5,userinfo:{first:first,last:last}}); }); function addtext(text){ // juts logs chatbox on screen     $('#messages').append($('<li>').text(text));      $('#console').scrolltop($('#console').prop("scrollheight")); } 

server

io.on('connection', function(socket){     var id = socket.id;     var publickey = null;     console.log('a user connected id '+id);     socket.emit('verifyid_1',{id:id});      socket.on('verifyid_2',function(data){             if (id != data.id) {console.log('___old client reconnects '+data.id);}         console.log('___double check '+id);         if (util.isexists(data.publickey)) { // function check if obj exists or not             socket.join(publickey); // if publickey exists, socket should join previous room identified publickey             listclientsinroom(publickey);         }     });     socket.on('disconnect', function(){         console.log('user#'+id+' disconnected '+util.isexists(io.sockets.adapter.rooms[publickey]));             if (!util.isexists(io.sockets.adapter.rooms[publickey])) return;         listclientsinroom(publickey);     });     socket.on('creategame',function(data){         var owner = {id:data.id, first:data.userinfo.first, last:data.userinfo.last};         var newpublickey = controller.addgamesession(data.numberofplayers,owner);          socket.join(newpublickey);          publickey = newpublickey;         socket.emit('gamecreated',{publickey:publickey});     });      socket.on('joinroom',function(data){         console.log("joinroom client "+id+" room: "+data.publickey);         socket.join(data.publickey); publickey = data.publickey;         listclientsinroom(publickey);           }); }); function listclientsinroom(key){   var clients = io.sockets.adapter.rooms[key].sockets;   console.log('___clients in room:'+json.stringify(clients)); } 

output on server when run 3 clients:

a user connected id /#mxzol2ibr9jrsmkiaaaa ___double check /#mxzol2ibr9jrsmkiaaaa user connected id /#ogt5s1ilxhp5tgy9aaab ___double check /#ogt5s1ilxhp5tgy9aaab user connected id /#59ozxn32hnbwzydvaaac ___double check /#59ozxn32hnbwzydvaaac joinroom client /#ogt5s1ilxhp5tgy9aaab room: m9xdowe7lxat088508 ___clients in room:{"/#mxzol2ibr9jrsmkiaaaa":true,"/#ogt5s1ilxhp5tgy9aaab":true} joinroom client /#59ozxn32hnbwzydvaaac room: m9xdowe7lxat088508 ___clients in room:{"/#mxzol2ibr9jrsmkiaaaa":true,"/#ogt5s1ilxhp5tgy9aaab":true,"/#59ozxn32hnbwzydvaaac":true} user#/#59ozxn32hnbwzydvaaac disconnected true ___clients in room:{"/#mxzol2ibr9jrsmkiaaaa":true,"/#ogt5s1ilxhp5tgy9aaab":true} user connected id /#m4k21ol_s493uvmpaaad ___double check /#m4k21ol_s493uvmpaaad ___clients in room:{"/#m4k21ol_s493uvmpaaad":true} 

what happened:

1. connect 3 clients hitting connect button (bt1). 2. client1 create new game (button bt5). save publickey locally. 3. client2 , client3 use publickey join room (button bt4) 4. client3 disconnect (button bt3). 5. client3 reconnect (button bt2) 6. id of client3 shown in room of publickey ???  

i believe re-iterating problem , code helps me find issue is.....:

if (util.isexists(data.publickey)) {      socket.join(data.publickey); // publickey local variable newly established socke         // when client reconnect variable should null default.         // should have used data.publickey given client instead. bad     listclientsinroom(data.publickey); }  

sorry. works now.


Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -