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 { 11 'type': 'websocket.accept', 12 'headers': [ 13 (b'x-subprotocols', str(subprotocols).encode()), 14 ], 15 'subprotocol': subprotocols[0], 16 } 17 ) 18 19 if m['type'] == 'websocket.receive': 20 await send( 21 { 22 'type': 'websocket.send', 23 'bytes': m.get('bytes', None), 24 'text': m.get('text', None), 25 } 26 ) 27 28 if m['type'] == 'websocket.disconnect': 29 break 30