且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Flask Socket IO中使用自定义ID作为会话ID

更新时间:2023-02-12 08:20:16

即使客户端重新连接,会话ID也应该是唯一的.应用程序无法选择这些ID,它们是服务器选择的随机数.

The session ids are supposed to be unique, even when a client reconnects. There is no way for an application to choose these ids, they are random numbers chosen by the server.

您需要做的是在连接事件中将会话ID映射到您的用户信息,并在断开事件中删除该映射.这样,您可以随时了解用户当前正在使用的会话ID.如果您需要特定的房间分配,则需要在连接时完成,而在断开连接时撤消.

What you need to do is map a session id to your user information in the connect event, and remove the mapping in the disconnect event. That way at any time you can find out what session id a user is currently on. If you need specific room assignments, those need to be done upon connection, and undone on disconnection.