socket.io client not receiving messages from server

后端 未结 2 2028
不思量自难忘°
不思量自难忘° 2020-12-14 06:23

I\'m trying to implement a system with two clients one of them sends a message and the other one shall receive it. The figure below will explain it in a more visual way:

相关标签:
2条回答
  • 2020-12-14 06:51

    The function passed to .on is called for each socket to do the initialization (binding events etc), and socket refers to that current socket. This will be client 1 when you receive a push message, because the handler function is bound to the push event of that socket - you bound that function when client 1 connected (where socket refers to client 1).

    io.sockets refers to all sockets connected, so including client 2 in your case.

    0 讨论(0)
  • 2020-12-14 07:00

    Another aspect which you could take into consideration is the use of :

    socket.broadcast.emit('push', { hello: 'world' });

    Which would essentially send the message to all the connected clients except the one that originated the message. Removing the task of filtering clients/reducing unnecessary traffic to the originating socket.

    Socket.IO on GitHub - Under broadcasting.

    0 讨论(0)
提交回复
热议问题