| | |
| | | import com.google.gson.Gson; |
| | | import com.google.gson.JsonSyntaxException; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.example.bitgetsclient.BitgetsClientApplication; |
| | | import org.example.bitgetsclient.comm.ApplicationContextProvider; |
| | | import org.example.bitgetsclient.util.RedisUtil; |
| | | import org.json.JSONException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.boot.SpringApplication; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ConfigurableApplicationContext; |
| | | import org.springframework.context.annotation.Bean; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.util.CollectionUtils; |
| | | |
| | | import javax.websocket.*; |
| | |
| | | import java.util.concurrent.Executors; |
| | | import java.util.concurrent.ScheduledExecutorService; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | |
| | | @ClientEndpoint |
| | | @Slf4j |
| | |
| | | |
| | | |
| | | |
| | | public void start() { |
| | | public void start() { |
| | | try { |
| | | connect(); // 尝试连接 |
| | | if (session == null) { |
| | |
| | | executorService.scheduleAtFixedRate(this::sendPing, PING_INTERVAL, PING_INTERVAL, TimeUnit.MILLISECONDS); |
| | | // 发送订阅消息 |
| | | session.getBasicRemote().sendText(subscriptions); // 发送订阅信息 |
| | | |
| | | synchronized (this) { |
| | | this.wait(); // 等待 WebSocket 消息到来 |
| | | } |
| | |
| | | if (dataNode != null) { |
| | | Map<String, Object> hashMap = new HashMap<>(); // 变量命名更具描述性 |
| | | |
| | | String bids = dataNode.get("bids").toString(); |
| | | String asks = dataNode.get("asks").toString(); |
| | | String asks = dataNode.get("bids").toString(); |
| | | String bids = dataNode.get("asks").toString(); |
| | | |
| | | Type listType = new TypeToken<List<List<String>>>(){}.getType(); |
| | | List<List<String>> bidsList = gson.fromJson(bids, listType); |
| | |
| | | } |
| | | |
| | | @OnClose |
| | | public void onClose() { |
| | | public void onClose() { |
| | | log.info("bitget ws 连接已关闭,尝试重新连接..."); // 记录连接关闭的信息 |
| | | handleConnectionClosedOrError(); // 处理连接关闭事件 |
| | | handleConnectionClosedOrError(); |
| | | } |
| | | |
| | | @OnError |
| | | public void onError(Throwable throwable) { |
| | | log.error("bitget ws 发生错误: " + throwable.getMessage(), throwable); // 记录错误信息 |
| | | handleConnectionClosedOrError(); // 处理错误事件 |
| | | handleConnectionClosedOrError(); |
| | | } |
| | | |
| | | private void handleConnectionClosedOrError() { |
| | | synchronized (lock) { |
| | | if (!reconnecting) { |
| | | reconnecting = true; // 开始重连 |
| | | executorService.execute(this::attemptReconnect); // 执行重连操作 |
| | | executorService.execute(() -> { |
| | | try { |
| | | attemptReconnect(); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | private void attemptReconnect() { |
| | |
| | | } |
| | | } else { |
| | | log.error("超过最大重连次数,停止重连"); // 超过最大重连次数 |
| | | reconnecting = false; // 重连状态重置 |
| | | } |
| | | } |
| | | |