一个简单的调试用WebSocket 服务端

最近在测试WebSocket,写了一个服务端

# server.py
import asyncio
import websockets

async def echo(websocket, path):
    async for message in websocket:
        print(f"Received message: {message}")  # 打印收到的消息
        await websocket.send(message)
        print(f"Sent message: {message}")  # 打印发送的消息

start_server = websockets.serve(echo, "localhost", 12345)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

 

posted @ 2024-06-27 20:09  夜歌乘年少  阅读(25)  评论(0编辑  收藏  举报