python - 如何断开自定义事件 python-socketio
问题描述
我有 2 个处理程序:
@sio.event
async def connect(sid, environ):
print('connect', sid)
@sio.event
async def disconnect(sid, environ):
# perform some user management stuff
# perform some cleaning as well
print('disconnect', sid)
我想触发disconnect
事件处理程序(因为我想在断开连接时执行一些特定操作),但是在一个名为leaveWorkbench
.
- 协程实际上
sio.disconnect()
调用了偶数处理程序disconnect
吗? - 我应该以这种方式发出事件
disconnect
:
@sio.event
async def leaveWorkbench(sid):
await sio.emit('disconnect')
(从文档看来,“保留”(?)不是一个好主意+它会被客户抓住,所以可能不是一个可行的解决方案)
- 还是我应该简单地将事件处理程序作为一种方法?使用
sio.discconect
async def disconnect_handler(sid):
# operations to be performed on disconnection
await sio.disconnect(sid)??
@sio.event
async def leaveWorkbench(sid):
await disconnect_handler(sid)
@sio.event
async def disconnect(sid):
await disconnect_handler(sid)
- 最后,还有其他“更清洁”的选择吗?重要说明:我在断开连接时使用该
session
对象
解决方案
正确的方法是调用sio.disconnect(sid)
.
但请注意,该disconnect()
方法只是启动断开连接,然后在后台发生。最终将调用受影响客户端的断开连接处理程序,但它可能不会立即调用(即可能需要一两秒钟)。客户端也将被通知它正在断开连接。
推荐阅读
- javascript - 除非选择特定变体,否则隐藏输入字段 - Shopify
- python - 在 Python 中的 API 网关请求中使用 AWS Cognito 访问令牌
- xslt-2.0 - 如何匹配元素中区分大小写的字符
- php - 如何在 PHP 中用已有的数字替换字符串
- cordova - Ioniv-v4:离子cordova插件添加cordova-plugin-googleplus不工作
- xcode - macOS NSExtensionMainStoryboard 无法加载
- ionic-framework - 如何在 ngSwitchCase 中加载离子图像?
- sql - 如何提高以下查询的性能
- python-3.x - couchbase 合并数组到子文档
- node.js - 这是 Node.js 项目的常见文件夹结构吗?