async def websocket_handler(request): ws = aiohttp.web.WebSocketResponse() await ws.prepare(request) await ws.send_str('Hello, client!') await ws.send_str('How are you?') return ws
async def websocket_handler(request): ws = aiohttp.web.WebSocketResponse() await ws.prepare(request) while True: msg = await ws.receive() if msg.type == aiohttp.WSMsgType.TEXT: await ws.send_str(f'You said: {msg.data}') elif msg.type == aiohttp.WSMsgType.ERROR: break return wsThis code defines a WebSocket handler that echoes back any text messages sent by the client. The receive() method is used to wait for a message from the client, and if the message type is TEXT, it is sent back to the client using the send_str() method. The package/library used in these examples is aiohttp.web.