| | |
| | | import com.alibaba.fastjson.JSONArray; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.xcong.excoin.modules.okxApi.wsHandler.OkxChannelHandler; |
| | | import com.xcong.excoin.modules.okxNewPrice.utils.SSLConfig; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.java_websocket.client.WebSocketClient; |
| | | import org.java_websocket.handshake.ServerHandshake; |
| | |
| | | import java.net.URI; |
| | | import java.net.URISyntaxException; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.Instant; |
| | | import java.time.ZoneId; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.util.ArrayList; |
| | | import java.util.Base64; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.TimeZone; |
| | | import java.util.concurrent.*; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | |
| | | /** 私有频道连接状态 */ |
| | | private final AtomicBoolean isPrivateConnected = new AtomicBoolean(false); |
| | | |
| | | /** 连接中标记,防重入 */ |
| | | private final AtomicBoolean isConnecting = new AtomicBoolean(false); |
| | | /** 公开频道连接中标记,防重入 */ |
| | | private final AtomicBoolean isPublicConnecting = new AtomicBoolean(false); |
| | | |
| | | /** 私有频道连接中标记,防重入 */ |
| | | private final AtomicBoolean isPrivateConnecting = new AtomicBoolean(false); |
| | | |
| | | /** 初始化标记,防重复 init */ |
| | | private final AtomicBoolean isInitialized = new AtomicBoolean(false); |
| | |
| | | String wsUrl = isPrivate ? config.getWsPrivateUrl() : config.getWsPublicUrl(); |
| | | String label = isPrivate ? "私有" : "公开"; |
| | | |
| | | if (isConnecting.get() || !isConnecting.compareAndSet(false, true)) { |
| | | AtomicBoolean connectingFlag = isPrivate ? isPrivateConnecting : isPublicConnecting; |
| | | if (connectingFlag.get() || !connectingFlag.compareAndSet(false, true)) { |
| | | log.info("[OKX-WS] 连接进行中,跳过重复{} WS请求", label); |
| | | return; |
| | | } |
| | |
| | | @Override |
| | | public void onOpen(ServerHandshake handshake) { |
| | | log.info("[OKX-WS] {} WS连接成功", label); |
| | | isConnecting.set(false); |
| | | connectingFlag.set(false); |
| | | |
| | | if (isPrivate) { |
| | | isPrivateConnected.set(true); |
| | |
| | | } else { |
| | | isPublicConnected.set(false); |
| | | } |
| | | isConnecting.set(false); |
| | | connectingFlag.set(false); |
| | | cancelPongTimeout(); |
| | | |
| | | if (sharedExecutor != null && !sharedExecutor.isShutdown() && !sharedExecutor.isTerminated()) { |
| | |
| | | @Override |
| | | public void onError(Exception ex) { |
| | | log.error("[OKX-WS] {} WS发生错误", label, ex); |
| | | connectingFlag.set(false); |
| | | if (isPrivate) { |
| | | isPrivateConnected.set(false); |
| | | } else { |
| | |
| | | } |
| | | } catch (URISyntaxException e) { |
| | | log.error("[OKX-WS] URI格式错误: {}", wsUrl, e); |
| | | isConnecting.set(false); |
| | | connectingFlag.set(false); |
| | | } |
| | | } |
| | | |