{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 })