python_websocket/webSocketServer_0.py

20 lines
537 B
Python

import websockets
import asyncio
import time
async def echo_server(websocket):
print("Client connected.")
try:
async for message in websocket:
print(f"Received: {message}")
response = f"Echo: {message}"
await websocket.send(response)
except websockets.exceptions.ConnectionClosed:
print("Client disconnected.")
async def main():
async with websockets.serve(echo_server, "10.0.0.202", 8788):
await asyncio.Future() # 永久运行
asyncio.run(main())