在 socket.io 中管理多个选项卡(但同一用户) [英] Manage multiple tabs (but same user) in socket.io

查看:20
本文介绍了在 socket.io 中管理多个选项卡(但同一用户)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 socket.io 时遇到了一些问题,我不知道如何解决.我有一个带有 socket.io 登录系统的应用程序来管理用户交互.另外我有一个数组来管理所有活动的 websocket 会话.该数组存储一对 session.id => 用户名.

当用户打开具有相同页面(或使用 websocket 的不同页面)的新选项卡时,问题就开始了.这会导致数组中出现重复的用户名条目.

{id1, username}{id2,用户名}

当socket向用户发送消息时,它只发送给数组的第一个会话ID,所以只有一个tab接收消息.

我尝试更改新连接的套接字 ID 以匹配另一个,但它不起作用.

socket.id = 另一个 websocket 的 sessionId;

我认为另一种解决方案是将消息发送到所有打开的会话,但我认为这不是最好的方法.

谢谢.

解决方案

听起来您可能指的是问题中的套接字 ID,而不是会话 ID.如果您有一个快速会话,您可以使用会话 ID,只要它们使用相同的浏览器并且不处于私人"模式,无论打开多少个选项卡,会话 ID 都是相同的.看看这个问题,了解如何在 socket.io 中启用会话.

socket.io 和 session?

更新:当您保存会话 ID 时,您会将其与用户的昵称和连接的套接字相关联.然后您可以遍历每个套接字并发送消息.类似于以下内容:

<预><代码>[{sessionId: '12345', 昵称: 'timmay!', socketIds: [1, 2, 3]},{sessionId:'23456',昵称:'pete',socketIds:[4, 5, 6]}]

当另一个连接建立时,您将新的套接字 ID 推送到 socketIds 数组中,当用户断开连接时,您将删除该 ID.

另一种选择可能是每个用户每个会话只允许一个选项卡.您可以存储单个 socketId,当同一个用户连接新的 socket 时,您可以断开原始 socket 并使用新的 socket.

I'm having some problems with socket.io, and I have no idea how to solve it. I've an app with a login system with socket.io to manage user interactions. Also I've an array to manage all active websocket sessions. This array stores a pair session.id => username.

Problems begin when a user opens a new tab with the same page (or a different page that uses the websocket). This cause a duplicate username entry in the array.

{id1, username}
{id2, username}

When socket sends a message to the user, it sends only to the first session id of the array, so only one tab receives the message.

I've tried to change the socket id of the new connection to match the other, but it doesn't work.

socket.id = sessionId of the other websocket;

I think another solution is to send the message to all the open sessions, but I think it's not the best way to do that.

Thanks.

解决方案

It sounds like you may be referring to the socket id in your question, not the session id. If you have an express session you can use the session id, which will be the same regardless of how many tabs are open as long as they use the same browser and it's not in "private" mode. Take a look at this question for how to enable sessions in socket.io.

socket.io and session?

Update: When you're saving the session id, you'll associate it with the user's nickname and the connected sockets. Then you can iterate through each socket and send the message. Something like the following:

[
    {sessionId: '12345', nickname: 'timmay!', socketIds: [1, 2, 3]},
    {sessionId: '23456', nickname: 'pete', socketIds: [4, 5, 6]}
]

When another connection is established you push the new socket id into the socketIds array, and when a user disconnects you remove the id.

Another option might be to only allow one tab per user per session. You can store a single socketId and when a new socket connects for the same user, you can disconnect the original socket and use the new one instead.

这篇关于在 socket.io 中管理多个选项卡(但同一用户)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆