基于Python Websockets的客户端程序,能够连接服务端、处理ping/pong、发送订阅请求并保持长连接
以下是一个基于Python Websockets的客户端程序,能够连接服务端、处理ping/pong、发送订阅请求并保持长连接:
import asyncio
import websockets
import json
import time
import logging
import traceback
from datetime import datetime, timedelta# 配置日志
logging.basicConfig(level=logging.INFO,format='%(asctime)s - %(levelname)s - %(message)s',handlers=[logging.FileHandler("websocket_client.log"),logging.StreamHandler()]
)
logger = logging.getLogger(__name__)class WebSocketClient:def __init__(self, uri, subscribe_message, max_reconnect_attempts=5):self.uri = uriself.subscribe_message = subscribe_messageself.max_reconnect_attempts = max_reconnect_attemptsself.reconnect_delay = 5 # 初始重连延迟(秒)self.last_message_time = time.time()self.connection_active = Falseself.websocket = Noneself.message_counter = 0self.ping_counter = 0self.pong_counter = 0async def connect(self):"""建立WebSocket连接并保持活动状态"""reconnect_attempts = 0while True:try:logger.info(f"Connecting to {self.uri}")async with websockets.connect(self.uri,ping_interval=20, # 发送底层ping的间隔ping_timeout=15, # 等待pong的超时时间close_timeout=5 # 关闭超时) as websocket:self.websocket = websocketself.connection_active = Truereconnect_attempts = 0self.reconnect_delay = 5logger.info("Connection established. Sending subscribe message...")await self.send_subscribe()# 启动消息处理任务await self.handle_messages()except (websockets.ConnectionClosed, ConnectionRefusedError) as e:logger.warning(f"Connection closed: {e}")self.connection_active = Falseexcept Exception as e:logger.error(f"Unexpected error: {str(e)}")logger.debug(traceback.format_exc())self.connection_active = Falsefinally:# 重连逻辑if reconnect_attempts