[Python] 纯文本查看 复制代码
import asyncio
import websockets
async def connect_to_websocket():
uri = "ws://127.0.0.1:9002"
try:
async with websockets.connect(uri) as websocket:
print("已成功连接到服务器")
# 发送一条消息到服务器
message = "Hello Server!"
await websocket.send(message)
print(f"发送: {message}")
# 接收服务器返回的消息
response = await websocket.recv()
print(f"接收: {response}")
except Exception as e:
print(f"连接或通信出现错误: {e}")
# 运行异步任务
asyncio.run(connect_to_websocket())
窗口程序集名 | 保 留 | 保 留 | 备 注 |
窗口程序集_启动窗口 | | | |
变量名 | 类 型 | 数组 | 备 注 |
ws | WebSocket服务器 | |
web | 网站服务器 | |
ws.
初始化 (web, 9002, &客户进入, &客户退出, &收到消息
)ws.
启动 ()调试输出 (“in”, 客户句柄
)调试输出 (“out”, 客户句柄
)|
收到消息 | | |
|
客户句柄 | 整数型 | | | |
消息类型 | 整数型 | | | | 消息句柄 | 整数型 | | | |
调试输出 (“来自” +
到文本 (客户句柄
) +
“ message:” + ws.
取回消息文本 (消息句柄
))