【发布时间】:2019-03-12 13:29:21
【问题描述】:
如果我不使用 websocket 的上下文,我将如何手动关闭它?当我尝试运行 conn.close() 时,我收到 event loop is closed 错误。这是我目前所拥有的:
from asgiref.sync import async_to_sync
import websockets
# connect -- works OK
conn = async_to_sync(websockets.connect)("ws://localhost:8000/ws/registration/123/")
# send message -- works OK
async_to_sync(conn.send)("hello")
# disconnect -- doesn't work
async_to_sync(conn.close)()
最后一部分应该是什么?
【问题讨论】:
-
你不使用
websockets.create_connection有什么原因吗? -
@madjaoue no -- 我对这个 websockets 库很陌生,所以任何建议都会很棒! -- 另外,我在支持的方法中没有看到:`'client'、'compatibility'、'connect'、'exceptions' `
-
你用哪个版本的python?和 websocket 客户端?
-
我从未使用过 websockets 库,但我每天都使用websocket-client,它似乎做得很好。您可以创建连接
ws = create_connection(someWsUrl),然后您可以使用ws.send、ws.recv或ws.close。我可以放一个小例子。在安装方面,pip install websocket-client,最后导入模块:from websocket import create_connection。希望对您有所帮助。 -
@madjaoue -- 哇,谢谢你的提示。我已经切换到那个库,它让事情变得更容易和直接了 100 倍。
标签: python python-3.x websocket django-channels