首页 > 解决方案 > 无尽的 Django websocket

问题描述

有一个页面有一个按钮,我可以单击该按钮并通过 Websocket 在服务器上启动该过程。我通过 Websocket 获取有关进程的信息。如何关闭并重新打开页面以查看进程的当前状态,而不是一开始就没有断开连接?

async def worker_status(send):
for i in range(numbers):
    some_function()
    await asyncio.sleep(1)

async def websocket_application(scope, receive, send):
while True:
    event = await receive()
    if event['type'] == 'websocket.connect':
        await send({
            'type': 'websocket.accept'
        })
    if event['type'] == 'websocket.disconnect':
        break
    if event['type'] == 'websocket.receive':
        if event['text'] == 'StartAct':
            loop = asyncio.get_event_loop()
            loop.create_task(worker_status())
            await send({
                'type': 'websocket.send',
                'text': str("In process")
            })
        if event['text'] == 'Status':
            await send({
                'type': 'websocket.send',
                'text': str("Ready to process")
            })

标签: djangowebsocket

解决方案


推荐阅读