【问题标题】:Get the the Flask-Security user id in a Flask-SocketIO event在 Flask-SocketIO 事件中获取 Flask-Security 用户 ID
【发布时间】:2021-01-22 23:13:49
【问题描述】:

this question 他们说,为了只向特定客户端广播socket.io 事件,您需要在某处将您的用户ID 和套接字会话ID 链接在一起。所以在我的套接字连接事件中,我想链接这两个 ID,但用户会话在 socket.io 事件中似乎不可用。有没有办法从这里的 Flask-Security/Login 访问用户 ID?

from flask_security import current_user
from app import socketio

socket_connections = {}

@socketio.on('connect')
def test_connect():
    print("Client connected: " + request.sid)

    # Prints None, even if authenticated
    print(current_user.get_id())

    # Link user ID with session ID
    socket_connections[current_user.get_id() = request.sid]

【问题讨论】:

    标签: python flask socket.io flask-login flask-socketio


    【解决方案1】:

    建立 Socket.IO 连接时的用户会话实际上可以在您的 Socket.IO 事件处理程序中访问,并且current_user 也可以访问。

    您没有显示您的代码,因此我无法告诉您出了什么问题,但我建议您尝试名为sessions.py 的 Socket.IO 示例,该示例专门用于帮助您了解用户会话在 Socket.IO 下的工作方式。我什至录制了一段视频,其中包含有关此的一些详细信息以及此示例应用程序的演示:

    https://www.youtube.com/watch?v=OZ1yQTbtf5E

    【讨论】:

      猜你喜欢
      • 2014-07-23
      • 2015-12-19
      • 2022-01-03
      • 2022-12-16
      • 2018-06-18
      • 1970-01-01
      • 2017-04-26
      • 2016-04-19
      • 1970-01-01
      相关资源
      最近更新 更多