Wednesday, 3 October 2018

Multiple Socket.io Connections from a Single Client

I tried to write a socket.io code where if a user click on any of the friend's id in the 'chat list', the socket parameter will match to the friend's id:$(function () { var friendId; var socket; $(document).on('click','.chat_list', function(e) { friendId = $(this).attr('id'); socket = io(`?token=${friendId}`); }); }); However, I found that every time I click on any of the friend's id in the list, the socket will fire up a new connection to the server. That is, if I click on the friend's id 3 times, there will be 3 socket connections to the server, all from the same clients.Is there a way to remove the previous connection before firing up a new one? The only way I can think of is to reload the entire page, or just do checking on the server-side.

Submitted October 03, 2018 at 03:48PM by VickNicks

No comments:

Post a Comment