Django 3.x 原生支持websocket 配置

{myproject}/websocket.py

1 # websocket.py 2 async def websocket_application(scope, receive, send): 3 while True: 4 event = await receive() 5 6 if event['type'] == 'websocket.connect': 7 await send({ 8 'type': 'websocket.accept' 9 }) 10 11 if event['type'] == 'websocket.disconnect': 12 break 13 14 if event['type'] == 'websocket.receive': 15 if event['text'] == 'ping': 16 await send({ 17 'type': 'websocket.send', 18 'text': 'pong!' 19 })

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdzfj.html