0

Server code:

   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);
    }

   });

Client code:

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;

  }
}

So what happens is I'm trying to run webrtc. I can connect the two people, and disconnect them. The reason I use different room names (room, and room3) is because otherwise the socket emits the disconnection to some other room, and disconnects another connection, not the one it's supposed to. So I'm just keeping track of rooms this way. Anyway, after it disconnects it goes to the quit, and then it prints out that message quite a bunch of times (that one on console.log where I said it's going to print it out quite a lot of times). If I go through debug in the browser and set a break point, it will pretty much go through it infinitely.

It tells me that it goes to the callback function in socket.io code... Here is the callback on socket.io:

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

And it just keeps going and going, and printing and printing out that one message... I have no idea why. I set quit to false, why can't it just stop?

Hellothere
  • 233
  • 3
  • 15
  • Add comments and say what you're trying to do when you use `socket.*`. Something is not clear... Example: why are you emitting `quit = true` to all clients in a room. – fdglefevre Mar 31 '15 at 19:33
  • I thought when I use "to" I emit it to all clients in a room BUT the one that emitted it... Since there are only two clients per room, only one other person should receive it... – Hellothere Mar 31 '15 at 19:45
  • No, read this: http://stackoverflow.com/q/10058226/4726998 – fdglefevre Mar 31 '15 at 19:56

1 Answers1

0

Try this and you will not send quit event to your own client:

socket.broadcast.to(room3).emit("quit", quit, room3);
fdglefevre
  • 672
  • 4
  • 15