Socket.io发送到不存在的房间

时间:2013-07-31 23:08:07

标签: javascript node.js socket.io

我正在编写游戏并使用socket.io。游戏中的每个地图都由socket.io中的房间代表。

它的工作原理如下。用户运行客户端并加入主房间(大厅),这是socket.io中的空房间“”,用户可以创建房间,roomId是数字,从0(0,1,2,3等)开始。当用户加入房间时,它会启动一个计时器 - 倒计时直到当前地图结束。

 __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();
    }
},

问题在于,当用户关闭他的客户端时,他离开了房间,如果房间里没有更多用户,则其被破坏。但是当用户再次运行客户端时,加入大厅,socket.io开始向大厅发送定时器事件。所以它散发到错误的房间。

日志:

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' ] }

2 个答案:

答案 0 :(得分:0)

我认为问题在于:“从0开始”。不要使用0作为房间名称。

答案 1 :(得分:0)

实际上,这是一个错误(?),你不能使用“数字”作为房间ID。只有字符串。