0

我正在编写游戏并使用 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' ] }
4

2 回答 2

1

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

于 2013-08-19T17:57:04.243 回答
0

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

于 2013-08-15T06:52:58.753 回答