如何在socket.io python服务器中获取连接客户端的会话ID? [英] How do I get the session ID of the connecting client in a socket.io python server?

查看:73
本文介绍了如何在socket.io python服务器中获取连接客户端的会话ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的东西:

from flask import Flask, jsonify, request, render_template, Response
from flask_socketio import SocketIO, emit
app = Flask(__name__)
socketio = SocketIO(app)

和:

@socketio.on('connect', namespace='/rv')
def connect():
# want to get session id or some client identifier

因为我想使用发出"和/或发送"回发给特定的客户端?

because I want to send back to a specific client with emit and/or send?

有人可以告诉我如何获取正确的会话/客户端标识符,然后如何使用它来发送/发送回消息吗?

Can someone show me how to get the right session/client identifier and then how to use it to send/emit a message back?

推荐答案

在1.x及更高版本中,您可以使用 request.sid 获得会话ID.将 emit()与参数 room = session_id 一起使用,以发送给特定的客户端.

At version 1.x and higher you can use request.sid to achieve the session ID. Use emit() with the argument room=session_id to emit to a specific client.

这篇关于如何在socket.io python服务器中获取连接客户端的会话ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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