如何在Flask Socket IO中使用自定义ID作为会话ID [英] How to use custom id as session id in Flask Socket IO
问题描述
我正在将flask socket-io用于基本的聊天应用程序.但是我有一个问题.当我的设备连接到服务器时,flask为设备提供一个会话ID.但是在断开连接并再次连接后,会话ID会更改.因此,此设备无法接收旧房间的消息.我想如果我将自定义会话ID分配给设备,那会是真的吗?或您有什么建议?
I am using flask socket-io for a basic chat app. But I have a problem. When my device connect to server, flask give to device a session id. But after disconnect and again connect, session id changes. So, this device can't receive old room's messages. I thought if I'd do if I assign custom session id to device.Is it true? Or what is your suggestions?
推荐答案
即使客户端重新连接,会话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.
这篇关于如何在Flask Socket IO中使用自定义ID作为会话ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!