io与私人房间聊天

io与私人房间聊天

本文介绍了socket.io与私人房间聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始研究node和socket.io。

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?

我读过0.7的新房间功能。那会是一条路吗?每次有2位用户需要私聊时动态创建一个新房间?但是,如何将创建的新房间通知第二个用户,以便他可以在那里连接?

I read on 0.7's new room feature. Would that 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?

谢谢

推荐答案

如果您想要的唯一功能是两个人能够彼此发送消息(而不是一群人有一个房间),那么逻辑可能类似于

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. 当用户连接时,将其连接存储在以其用户名作为键的对象中(或存储在确保您可以找到特定用户的连接)。

  2. 当Bob想要与Jeff交谈时,向服务器发送一个事件,说明该情况。

  3. 服务器会查找第一步中对象中Jeff的Socket.IO连接。

  4. 服务器使用此连接向Jeff(且仅Jeff)发送私人消息。

  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.

这篇关于socket.io与私人房间聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 16:08