Sending message to a specific connected users using webSocket?

后端 未结 5 2131
Happy的楠姐
Happy的楠姐 2020-11-29 15:43

I wrote a code for broadcasting a message to all users:

// websocket and http servers
var webSocketServer = require(\'websocket\').server;

...
...
v         


        
5条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-29 16:15

    This is not only the right way to go, but the only way. Basically each connection needs a unique ID. Otherwise you won't be able to identify them, it's as simple as that.

    Now how you will represent it it's a different thing. Making an object with id and connection properties is a good way to do that ( I would definitely go for it ). You could also attach the id directly to connection object.

    Also remember that if you want communication between users, then you have to send target user's ID as well, i.e. when user A wants to send a message to user B, then obviously A has to know the ID of B.

提交回复
热议问题