socket.io 与私人房间聊天

发布于 2024-12-04 16:39:49 字数 282 浏览 1 评论 0原文

我开始研究node和socket.io。

我已经创建了一个简单的聊天应用程序,我惊讶于它是如此简单。

现在,我想更进一步,提供能够私下聊天的在线用户列表。

解决这个问题的最佳方法是什么?

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

自己处理上述所有逻辑是否更好?将房间和用户存储在服务器端并每次循环遍历它们并向适当的房间和用户发送消息?

谢谢

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 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?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舞袖。长 2024-12-11 16:39:49

如果您想要的唯一功能是两个人能够互相发送消息(而不是一群人拥有一个房间),那么逻辑可能是这样的:

  1. 当用户连接时,将他们的连接存储在由用户名(或确保您可以找到特定用户的连接的任何其他数据结构)键入的对象。
  2. 当鲍勃想要与杰夫交谈时,向服务器发送一个事件来说明这一点。
  3. 服务器在步骤 1 中的对象中查找 Jeff 的 Socket.IO 连接。
  4. 服务器使用此连接向 Jeff(且仅向 Jeff)发送私人消息。

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.
世界等同你 2024-12-11 16:39:49

Hej Thomas,

如果只有 2 个用户在说话,则不需要使用发布,只需将该消息从客户端发送到服务器,然后让服务器找到另一个客户端并将其发送下来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文