import asyncio
import websockets


# 定义处理 WebSocket 连接的协程
async def handle_connection(websocket, path):
    # 接收客户端发送的消息
    print("WebSocket connection established.")
    while True:
        message = await websocket.recv()
        print(f"Received message: {message}")

    # # # 发送响应消息给客户端
    # response = f"Hello, {message}!"
    # await websocket.send(response)


# 启动 WebSocket 服务端
start_server = websockets.serve(handle_connection, "192.168.0.111", 8765)

# 运行事件循环
asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()
