如何解决Sanic Websockets连接
我尝试从我的http路由发送json数据,这样做有一些逻辑,并在完成操作后将数据发送到websocket通道。这个Websocket通道发送数据和前端监听更改。
@app.route("/text",methods=["POST"])
async def handler_text(request):
#some logic manipulations
ws = await websockets.client.connect('ws://localhost:5003/backend/alert/message')
print('Connected to alert ws')
await ws.send(json({'message': 'Create new data from db'}))
return response.text("Hello")
@app.websocket("/message")
async def handler_ws(request,ws):
print("Websocket opened")
name = "<someone>"
while True:
data = f"Hello {name}"
await ws.send(data)
name = await ws.recv()
if not name:
break
但是当我发送发布请求时,它尝试连接,但进程在连接时刻停止。 在'/ test'端点上发送数据后,我尝试不等待而使用关闭连接。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。