java.lang.Object
org.java_websocket.WebSocketAdapter
org.java_websocket.AbstractWebSocket
org.java_websocket.client.WebSocketClient
io.github.theword.queqiao.core.websocket.WsClient
All Implemented Interfaces:
Runnable, org.java_websocket.WebSocket, org.java_websocket.WebSocketListener

public class WsClient extends org.java_websocket.client.WebSocketClient
WebSocket 客户端

重连生命周期由本类自己掌握,不依赖 Java-WebSocket 的行为细节:

  • 唯一入口:所有重连请求(自动 / 手动)都经 requestReconnect(ReconnectReason)
  • generation:让已入队但过期的任务自行失效(Future.cancel 拦不住已开始执行的任务)
  • reconnectInProgress:非阻塞互斥,防止同一实例并发执行 super.reconnect()
  • stopped:唯一可靠的"是否还需要维持连接"判据,取代不可靠的 remote 参数

所有权:调度器由 WebsocketManager 提供并持有,本类只使用、绝不销毁它。

Since:
0.6.11
  • Field Summary

    Fields inherited from class org.java_websocket.client.WebSocketClient

    uri
  • Constructor Summary

    Constructors
    Constructor
    Description
    WsClient(URI uri, org.slf4j.Logger logger, ScheduledExecutorService reconnectScheduler, ReconnectPolicy reconnectPolicy, HandleProtocolMessage handleProtocolMessage, String serverName, String accessToken, boolean enabled)
    构造 WebSocket 客户端
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    获取当前连续失败次数(诊断与测试用)
    boolean
    是否已有重连正在执行(诊断与测试用)
    boolean
    是否已停止(诊断与测试用)
    void
    onClose(int code, String reason, boolean remote)
    连接关闭
    void
    onError(Exception exception)
    连接异常
    void
    onMessage(String message)
    收到消息
    void
    onOpen(org.java_websocket.handshake.ServerHandshake serverHandshake)
    连接成功
    void
    主动立即重连(手动重连)
    void
    stopWithoutReconnect(int code, String reason)
    停止并不再自动重连

    Methods inherited from class org.java_websocket.client.WebSocketClient

    addHeader, clearHeaders, close, close, close, closeBlocking, closeConnection, connect, connectBlocking, connectBlocking, getAttachment, getConnection, getConnections, getDraft, getLocalSocketAddress, getLocalSocketAddress, getProtocol, getReadyState, getRemoteSocketAddress, getRemoteSocketAddress, getResourceDescriptor, getSocket, getSSLSession, getURI, hasBufferedData, hasSSLSupport, isClosed, isClosing, isFlushAndClose, isOpen, onCloseInitiated, onClosing, onMessage, onSetSSLParameters, onWebsocketClose, onWebsocketCloseInitiated, onWebsocketClosing, onWebsocketError, onWebsocketMessage, onWebsocketMessage, onWebsocketOpen, onWriteDemand, reconnect, reconnectBlocking, removeHeader, run, send, send, send, sendFragmentedFrame, sendFrame, sendFrame, sendPing, setAttachment, setDnsResolver, setProxy, setSocket, setSocketFactory

    Methods inherited from class org.java_websocket.AbstractWebSocket

    getConnectionLostTimeout, isReuseAddr, isTcpNoDelay, setConnectionLostTimeout, setReuseAddr, setTcpNoDelay, startConnectionLostTimer, stopConnectionLostTimer

    Methods inherited from class org.java_websocket.WebSocketAdapter

    onPreparePing, onWebsocketHandshakeReceivedAsClient, onWebsocketHandshakeReceivedAsServer, onWebsocketHandshakeSentAsClient, onWebsocketPing, onWebsocketPong

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • WsClient

      public WsClient(URI uri, org.slf4j.Logger logger, ScheduledExecutorService reconnectScheduler, ReconnectPolicy reconnectPolicy, HandleProtocolMessage handleProtocolMessage, String serverName, String accessToken, boolean enabled)
      构造 WebSocket 客户端
      Parameters:
      uri - 连接地址
      logger - 日志实现
      reconnectScheduler - 由 WebsocketManager 提供的共享重连调度器
      reconnectPolicy - 退避策略
      handleProtocolMessage - 协议分发入口(由 QueQiaoRuntime 创建,多连接共享同一实例)
      serverName - 服务器名称
      accessToken - 访问令牌(为空表示不鉴权)
      enabled - 是否启用协议消息处理
  • Method Details

    • onOpen

      public void onOpen(org.java_websocket.handshake.ServerHandshake serverHandshake)
      连接成功

      必须同时完成四件事:推进代际、取消待执行任务、清空任务引用、重置连续失败计数。 否则一个"迟到的"自动重连任务会把刚建立好的健康连接 reset() 掉,造成连接抖动。

      Specified by:
      onOpen in class org.java_websocket.client.WebSocketClient
      Parameters:
      serverHandshake - 握手信息
    • onMessage

      public void onMessage(String message)
      收到消息

      业务层异常绝不允许逃出 WebSocket callback:Java-WebSocket 的读循环会捕获 RuntimeException 并执行 closeConnection(ABNORMAL_CLOSE), 一条异常消息就会把连接打掉并触发无意义的连接抖动。

      Specified by:
      onMessage in class org.java_websocket.client.WebSocketClient
      Parameters:
      message - 原始消息
    • onClose

      public void onClose(int code, String reason, boolean remote)
      连接关闭

      不依赖 remote:Java-WebSocket 在连接失败、异常关闭、心跳超时时 都会以 remote == false 进入本回调。唯一可靠的判据是 stopped。

      Specified by:
      onClose in class org.java_websocket.client.WebSocketClient
      Parameters:
      code - 关闭码
      reason - 关闭原因
      remote - 是否由对端发起(不可作为重连判据)
    • onError

      public void onError(Exception exception)
      连接异常

      只记录错误,不安排重连:连接失败与异常关闭都会随后触发 onClose, 由 onClose 走统一 pipeline。否则一次故障会被安排两次重连。

      Specified by:
      onError in class org.java_websocket.client.WebSocketClient
      Parameters:
      exception - 异常
    • reconnectNow

      public void reconnectNow()
      主动立即重连(手动重连)

      与自动重连共用同一 pipeline,只是延迟为 0 且会重置连续失败计数。

    • stopWithoutReconnect

      public void stopWithoutReconnect(int code, String reason)
      停止并不再自动重连

      绝不 shutdown 共享调度器:它是 WebsocketManager 的资源, 单个 Client 的停止不能影响其它 Client。

      Parameters:
      code - 关闭码
      reason - 关闭原因
    • isReconnectInProgress

      public boolean isReconnectInProgress()
      是否已有重连正在执行(诊断与测试用)
      Returns:
      true 表示当前有 reconnect 正在执行
    • getReconnectAttempts

      public int getReconnectAttempts()
      获取当前连续失败次数(诊断与测试用)

      语义边界:该计数在决定重连时自增(requestReconnect(io.github.theword.queqiao.core.websocket.ReconnectReason) 内), 早于"任务被提交到调度器",更早于"调度线程真正启动"。 因此它只能回答"是否已决定重连",不能作为"重连已安排/线程已存在"的证据—— 需要后两者时请等待 scheduler 的状态或实际观测。

      Returns:
      连续失败次数
    • isStopped

      public boolean isStopped()
      是否已停止(诊断与测试用)
      Returns:
      true 表示已停止,不会再自动重连