作者:itsima | 来源:互联网 | 2023-01-25 15:57
Asfarasweknowcreationofsocketsisuniqueid.whenmyuserscreateroomandinviteuserstojoi
As far as we know creation of sockets is unique id.when my users create room and invite users to join into that. I cant save and store joined users on rooms.when user joined on room (s)he likes to server store this join and after disconnet and connected again joined automatically to latest joined rooms.how can i store sockets and join automatically users?
据我们所知,套接字的创建是唯一的id。当我的用户创建空间并邀请用户加入时。我不能保存和存储加入的用户在房间。当用户加入房间时,他喜欢服务器存储此加入,并在disconnet和连接后再次自动加入到最新加入的房间。我可以存储套接字并自动加入用户吗?
I cant find any solution about this ability. Because sockets changed on each connection.
我无法找到有关此能力的任何解决方案。因为每个连接上的套接字都已更改。
Is any simple solution? Thanks in advance
有简单的解决方案吗?提前致谢
1 个解决方案
1
You need to create some sort of userID for each user and, usually store that in a COOKIE. Then, if you want persistent rooms, you have to create your own server-side data structure that keeps track of which rooms you want each user to be in. And, each time you add or remove a user from a room, you can modify your own data structure so it is properly keeping track of that state.
您需要为每个用户创建某种userID,并且通常将其存储在COOKIE中。然后,如果您想要持久性房间,您必须创建自己的服务器端数据结构,以跟踪您希望每个用户所在的房间。并且,每次从房间添加或删除用户时,您都可以修改您自己的数据结构,以便正确跟踪该状态。
Then, when a user connects, you can get their userID from the COOKIE and look in your data structure to see what rooms you should put that connection in. Then, to keep memory management in check, you have to somehow expire users from your data structure after some period of inactivity so they don't stay in there forever. (probably you timestamp when they last visited and then hourly you walk through the data structure and remove users who haven't visited in awhile).
然后,当用户连接时,您可以从COOKIE获取其用户ID并查看您的数据结构以查看您应该将该连接放入哪些房间。然后,为了检查内存管理,您必须以某种方式使用户的数据到期经过一段时间不活动后的结构,所以他们不会永远留在那里。 (可能是你上次访问时的时间戳,然后每小时你遍历数据结构并删除一段时间没有访问过的用户)。
If you want this information to also survive a server restart, then you have to save it persistently to disk and then reload that information upon startup. If you're already using a database as part of your app, you can store the info in a database or, if not, you can just write your internal data structure out to disk every so often, probably as JSON and then read that in when your server starts.
如果您希望此信息在服务器重新启动后仍然存在,则必须将其永久保存到磁盘,然后在启动时重新加载该信息。如果您已经将数据库用作应用程序的一部分,则可以将信息存储在数据库中,否则,您可以将内部数据结构尽可能多地写入磁盘,可能是JSON,然后在当您的服务器启动时。