I'm writing a game and using socket.io. Every map in the game is represented by a room in socket.io.
It works like this. User runs a client and joins main room (lobby) which is an empty room "" in socket.io, From there, user can create a room, roomId is number, starting from 0 (0,1,2,3, etc). When user joins a room, its starts a timer - countdown till the end of the current map.
__startTimer:function()
{
this.__timerId=setInterval(this.__onTimerTick.bind(this), 1000);
},
__onTimerTick:function()
{
var string="Timer event in room "+this.__roomId+", list of socket.io rooms:";
console.log(string);
console.log(g.socketIo.sockets.manager.rooms);
this.__duration--;
g.socketIo.sockets.in(this.__roomId).emit(g.messages.FIGHT_TIMER_TICK_EVENT, {time: this.__duration});
if(this.__duration==0)
{
clearInterval(this.__timerId);
this.__fightEnd();
}
},
The problem is that, when users close his client, he leaves the room, and if there is no more users in room, its destroyed. BUT when user runs client again, joins lobby, socket.io starts emitting timer event to lobby room. So its emiting to the wrong room.
Log:
info: socket.io started
debug: client authorized
info: handshake authorized iFE6tqsa_lThtxv8wsoi
debug: setting request GET /socket.io/1/flashsocket/iFE6tqsa_lThtxv8wsoi
debug: set heartbeat interval for client iFE6tqsa_lThtxv8wsoi
debug: client authorized for
Timer event in room 0, list of socket.io rooms:
{ '': [ 'iFE6tqsa_lThtxv8wsoi' ],
'/0': [ 'iFE6tqsa_lThtxv8wsoi' ] }
debug: flashsocket writing 5:::{"name":"104","args":[{"time":179}]}
Timer event in room 0, list of socket.io rooms:
{ '': [ 'iFE6tqsa_lThtxv8wsoi' ],
'/0': [ 'iFE6tqsa_lThtxv8wsoi' ] }
debug: flashsocket writing 5:::{"name":"104","args":[{"time":178}]}
Timer event in room 0, list of socket.io rooms:
{ '': [ 'iFE6tqsa_lThtxv8wsoi' ],
'/0': [ 'iFE6tqsa_lThtxv8wsoi' ] }
debug: flashsocket writing 5:::{"name":"104","args":[{"time":177}]}
info: transport end (socket end)
debug: set close timeout for client iFE6tqsa_lThtxv8wsoi
debug: cleared close timeout for client iFE6tqsa_lThtxv8wsoi
debug: cleared heartbeat interval for client iFE6tqsa_lThtxv8wsoi
debug: discarding transport
Timer event in room 0, list of socket.io rooms:
{}
Timer event in room 0, list of socket.io rooms:
{}
Timer event in room 0, list of socket.io rooms:
{}
Timer event in room 0, list of socket.io rooms:
{}
debug: client authorized
info: handshake authorized z540AkrvWAb5X4Lzwsoj
debug: setting request GET /socket.io/1/flashsocket/z540AkrvWAb5X4Lzwsoj
debug: set heartbeat interval for client z540AkrvWAb5X4Lzwsoj
debug: client authorized for
Timer event in room 0, list of socket.io rooms:
{ '': [ 'z540AkrvWAb5X4Lzwsoj' ] }
debug: flashsocket writing 5:::{"name":"104","args":[{"time":158}]}
Timer event in room 0, list of socket.io rooms:
{ '': [ 'z540AkrvWAb5X4Lzwsoj' ] }
i think the problem is in this: "starting from 0". Dont use 0 for room name.
Actually, it's a bug (?), you cant use "Number" as a room id. Only string.