1async def application(scope, receive, send): 2 assert scope['type'] == 'websocket' 3 4 while True: 5 m = await receive() 6 if m['type'] == 'websocket.connect': 7 subprotocols = scope['subprotocols'] 8 9 await send({ 10 'type': 'websocket.accept', 11 'headers': [ 12 (b'x-subprotocols', str(subprotocols).encode()), 13 ], 14 'subprotocol': subprotocols[0], 15 }) 16 17 if m['type'] == 'websocket.receive': 18 await send({ 19 'type': 'websocket.send', 20 'bytes': m.get('bytes', None), 21 'text': m.get('text', None), 22 }) 23 24 if m['type'] == 'websocket.disconnect': 25 break; 26