| | |
| | | * |
| | | * @author Administrator |
| | | */ |
| | | @SuppressWarnings("ALL") |
| | | @Slf4j |
| | | public class GateKlineWebSocketClient { |
| | | |
| | |
| | | */ |
| | | public void init() { |
| | | if (!isInitialized.compareAndSet(false, true)) { |
| | | log.warn("[WS] already init, skip"); |
| | | log.warn("[WS] 已初始化过,跳过重复初始化"); |
| | | return; |
| | | } |
| | | connect(); |
| | |
| | | * 避免 onClose 回调中的 reconnectWithBackoff 访问已关闭的线程池。 |
| | | */ |
| | | public void destroy() { |
| | | log.info("[WS] destroy..."); |
| | | log.info("[WS] 开始销毁..."); |
| | | |
| | | if (webSocketClient != null && webSocketClient.isOpen()) { |
| | | for (GateChannelHandler handler : channelHandlers) { |
| | |
| | | Thread.sleep(500); |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | log.warn("[WS] unsubscribe wait interrupted"); |
| | | log.warn("[WS] 取消订阅等待被中断"); |
| | | } |
| | | } |
| | | |
| | |
| | | webSocketClient.closeBlocking(); |
| | | } catch (InterruptedException e) { |
| | | Thread.currentThread().interrupt(); |
| | | log.warn("[WS] close interrupted"); |
| | | log.warn("[WS] 关闭连接时被中断"); |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | shutdownExecutorGracefully(sharedExecutor); |
| | | |
| | | log.info("[WS] destroyed"); |
| | | log.info("[WS] 销毁完成"); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | private void connect() { |
| | | if (isConnecting.get() || !isConnecting.compareAndSet(false, true)) { |
| | | log.info("[WS] already connecting"); |
| | | log.info("[WS] 连接进行中,跳过重复请求"); |
| | | return; |
| | | } |
| | | try { |
| | |
| | | webSocketClient = new WebSocketClient(uri) { |
| | | @Override |
| | | public void onOpen(ServerHandshake handshake) { |
| | | log.info("[WS] connected"); |
| | | log.info("[WS] 连接成功"); |
| | | isConnected.set(true); |
| | | isConnecting.set(false); |
| | | if (sharedExecutor != null && !sharedExecutor.isShutdown()) { |
| | |
| | | } |
| | | sendPing(); |
| | | } else { |
| | | log.warn("[WS] shutting down, ignore onOpen"); |
| | | log.warn("[WS] 应用正在关闭,忽略连接成功回调"); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | @Override |
| | | public void onClose(int code, String reason, boolean remote) { |
| | | log.warn("[WS] closed, code:{}, reason:{}", code, reason); |
| | | log.warn("[WS] 连接关闭, code:{}, reason:{}", code, reason); |
| | | isConnected.set(false); |
| | | isConnecting.set(false); |
| | | cancelPongTimeout(); |
| | | if (sharedExecutor != null && !sharedExecutor.isShutdown() && !sharedExecutor.isTerminated()) { |
| | | sharedExecutor.execute(() -> { |
| | | try { reconnectWithBackoff(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { log.error("[WS] reconnect fail", e); } |
| | | try { reconnectWithBackoff(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } catch (Exception e) { log.error("[WS] 重连失败", e); } |
| | | }); |
| | | } else { |
| | | log.warn("[WS] executor closed, no reconnect"); |
| | | log.warn("[WS] 线程池已关闭,不执行重连"); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onError(Exception ex) { |
| | | log.error("[WS] error", ex); |
| | | log.error("[WS] 发生错误", ex); |
| | | isConnected.set(false); |
| | | } |
| | | }; |
| | | webSocketClient.connect(); |
| | | } catch (URISyntaxException e) { |
| | | log.error("[WS] bad uri", e); |
| | | log.error("[WS] URI格式错误", e); |
| | | isConnecting.set(false); |
| | | } |
| | | } |
| | |
| | | String event = response.getString("event"); |
| | | |
| | | if (FUTURES_PONG.equals(channel)) { |
| | | log.debug("[WS] pong received"); |
| | | log.debug("[WS] 收到 pong 响应"); |
| | | cancelPongTimeout(); |
| | | return; |
| | | } |
| | | if ("subscribe".equals(event)) { |
| | | log.info("[WS] {} subscribed: {}", channel, response.getJSONObject("result")); |
| | | log.info("[WS] {} 订阅成功: {}", channel, response.getJSONObject("result")); |
| | | return; |
| | | } |
| | | if ("unsubscribe".equals(event)) { |
| | | log.info("[WS] {} unsubscribed", channel); |
| | | log.info("[WS] {} 取消订阅成功", channel); |
| | | return; |
| | | } |
| | | if ("error".equals(event)) { |
| | | JSONObject error = response.getJSONObject("error"); |
| | | log.error("[WS] {} error, code:{}, msg:{}", |
| | | log.error("[WS] {} 错误, code:{}, msg:{}", |
| | | channel, |
| | | error != null ? error.getInteger("code") : "N/A", |
| | | error != null ? error.getString("message") : response.getString("msg")); |
| | |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | log.error("[WS] handle msg fail: {}", message, e); |
| | | log.error("[WS] 处理消息失败: {}", message, e); |
| | | } |
| | | } |
| | | |
| | |
| | | pingMsg.put("time", System.currentTimeMillis() / 1000); |
| | | pingMsg.put("channel", FUTURES_PING); |
| | | webSocketClient.send(pingMsg.toJSONString()); |
| | | log.debug("[WS] ping sent"); |
| | | log.debug("[WS] 发送 ping 请求"); |
| | | } |
| | | } catch (Exception e) { log.warn("[WS] ping fail", e); } |
| | | } catch (Exception e) { log.warn("[WS] 发送 ping 失败", e); } |
| | | } |
| | | |
| | | private synchronized void cancelPongTimeout() { |
| | |
| | | int attempt = 0, maxAttempts = 3; |
| | | long delayMs = 5000; |
| | | while (attempt < maxAttempts) { |
| | | try { Thread.sleep(delayMs); connect(); return; } catch (Exception e) { log.warn("[WS] reconnect attempt {} fail", attempt + 1, e); delayMs *= 2; attempt++; } |
| | | try { Thread.sleep(delayMs); connect(); return; } catch (Exception e) { log.warn("[WS] 第{}次重连失败", attempt + 1, e); delayMs *= 2; attempt++; } |
| | | } |
| | | log.error("[WS] reconnect exhausted after {} attempts", maxAttempts); |
| | | log.error("[WS] 超过最大重试次数({}),放弃重连", maxAttempts); |
| | | } |
| | | |
| | | private void shutdownExecutorGracefully(ExecutorService executor) { |