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 (将#修改为@)