Socket.io回调经历了无限循环......为什么?

时间:2015-03-31 06:07:17

标签: javascript node.js socket.io webrtc p2p

服务器代码:

			socket.once("quit", function(quit, room3)
			{

				console.log("This is quit: ", quit);
				if (quit == true)
				{
					socket.to(room3).emit("quit", quit, room3);
					socket.leave(room3);
					console.log("EMITTING TRUE QUIT");
					console.log("THIS IS ROOM:", room3);
				}
				else if (quit == false)
				{
					console.log("Quit is false");
					console.log("False quit, room: ", room3);
				}

			});

客户代码:

socket.on("quit", function(quit, room3)
		{
			console.log("This is room on socket quit: ", room3);
			console.log("This is roomm 1: ", room);
			if (quit == true && pc != null)
			{
				pc.close();
				pc = null;
				quit = false;
				socket.emit("quit", quit, room2);
			}
			else if(quit == false)
			{
				console.log("quit is false right now.");
			}
			else
			{
				console.log("This message is going to appear quite a lot of times on the console. Why the infinite loop?");
			}

			console.log("GOT QUIT", quit);
		});
		
document.getElementById("disconnect").addEventListener("click", function(e)
                                                       {

  if (pc != null)
  {
    console.log("The person clicked disconnect!");
    quit = true;
    console.log("This is the room on disconnect: ", room);
    socket.emit("quit", quit, room);
    pc.close();
    pc = null;
    document.getElementById("connect").disabled = false;

  }
}

所以会发生什么,我正在尝试运行webrtc。我可以连接两个人,并断开它们。我使用不同房间名称(房间和房间3)的原因是因为否则套接字会断开与其他房间的连接,并断开另一个连接,而不是它应该连接的连接。所以我只是这样跟踪房间。 无论如何,在它断开连接之后它会退出,然后它打印出那条消息很多次(在console.log上的那个消息,我说它会打印出来很多次)。如果我在浏览器中进行调试并设置一个断点,它将无限地通过它。

它告诉我它转到socket.io代码中的回调函数......这是socket.io上的回调:

 if (callbacks) {
callbacks = callbacks.slice(0);
for (var i = 0, len = callbacks.length; i < len; ++i) {
  callbacks[i].apply(this, args);
}
}

它只是继续前进,打印和打印出一条消息......我不明白为什么。我设置退出假,为什么不能停止?

1 个答案:

答案 0 :(得分:0)

尝试此操作,您不会将quit事件发送给您自己的客户:

socket.broadcast.to(room3).emit("quit", quit, room3);