开发者

socket.io chat with private rooms

开发者 https://www.devze.com 2023-04-05 03:43 出处:网络
I started looking into node and socket.io. I already have created a simple chat application and I am amazed at how easy it was.

I started looking into node and socket.io.

I already have created a simple chat application and I am amazed at how easy it was.

Now, I would like to take a little bit further and provide a list of online users that have the ability to chat with each other in private.

What would be the best way to approach this?

I read on 0.7's new room feature. Would 开发者_如何学JAVAthat be a way to go? Dynamically create a new room each time 2 users need to chat in private? But how the second user is going to be notified of the new room created, so that he can connect there?

Is it better to handle all the above logic myself? Store the rooms and users server side and loop through them each time and send messages to the appropriate ones?

Thanks


If the only functionality you want is for two people to be able to send messages to one another (and not groups of people to have a room), then the logic could be something like this:

  1. When a user connects, store their connection in an object keyed by their username (or in any other data structure that ensures you can find a specific user's connection).
  2. When a Bob wants to talk to Jeff, send the server an event stating such.
  3. The server looks up Jeff's Socket.IO connection in the object from step 1.
  4. The server uses this connection to send Jeff (and only Jeff) the private message.


Hej Thomas

if theres only 2 users talking you dont need use publish att all just send that message from the client to the server and let the server locate the other client and send it down.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号