1
zj
14 hours ago ed02adce53d9f51287e14764815006dd4d040daf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package com.nq.ws;
 
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nq.enums.EStockType;
import com.nq.pojo.StockRealTimeBean;
import com.nq.utils.redis.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
 
import java.io.IOException;
import java.lang.reflect.Type;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
 
@Slf4j
public class WebsocketRunClient extends WebSocketClient {
 
    private static final int HTTP_CONNECT_TIMEOUT_MS = 2000;
    private static final int HTTP_SOCKET_TIMEOUT_MS = 3000;
 
    private static final CloseableHttpClient HTTP_CLIENT = HttpClients.custom()
            .setDefaultRequestConfig(RequestConfig.custom()
                    .setConnectTimeout(HTTP_CONNECT_TIMEOUT_MS)
                    .setConnectionRequestTimeout(HTTP_CONNECT_TIMEOUT_MS)
                    .setSocketTimeout(HTTP_SOCKET_TIMEOUT_MS)
                    .build())
            .setMaxConnTotal(20)
            .setMaxConnPerRoute(10)
            .build();
 
    private final EStockType eStockType;
    private Timer heartbeatTimer;
    private volatile boolean localNotifyEnabled = true;
 
    public WebsocketRunClient(URI serverUri, EStockType eStockType) {
        super(serverUri);
        this.eStockType = eStockType;
    }
 
    private void startHeartbeat() {
        stopHeartbeat();
        heartbeatTimer = new Timer("ws-heartbeat-" + eStockType.getCode(), true);
        heartbeatTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!isOpen()) {
                    return;
                }
                try {
                    sendAuthKey();
                } catch (Exception e) {
                    log.warn("websocket {} 心跳失败: {}", eStockType.getCode(), e.getMessage());
                }
            }
        }, 0, 3000);
    }
 
    private void stopHeartbeat() {
        if (heartbeatTimer != null) {
            heartbeatTimer.cancel();
            heartbeatTimer = null;
        }
    }
 
    private void sendAuthKey() {
        send(("key:" + eStockType.getStockKey() + ":" + eStockType.getContryId()).getBytes());
    }
 
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        sendAuthKey();
        startHeartbeat();
    }
 
    @Override
    public void onMessage(String s) {
        if ("pong".equals(s) || "身份验证成功".equals(s)) {
            log.debug("websocket {} 握手/心跳: {}", eStockType.getCode(), s);
            return;
        }
        try {
            Map<String, String> stringObjectMap = jsonToMap(s);
            StockRealTimeBean stockRealTimeBean = new StockRealTimeBean();
            stockRealTimeBean.setPid(stringObjectMap.get("Id"));
            stockRealTimeBean.setLast(stringObjectMap.get("Last"));
            stockRealTimeBean.setBid(stringObjectMap.get("Bid"));
            stockRealTimeBean.setAsk(stringObjectMap.get("Ask"));
            stockRealTimeBean.setHigh(stringObjectMap.get("High"));
            stockRealTimeBean.setLow(stringObjectMap.get("Low"));
            stockRealTimeBean.setPc(stringObjectMap.get("Chg"));
            stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct") + "%");
            stockRealTimeBean.setTime(stringObjectMap.get("Time"));
            RedisKeyUtil.setCacheRealTimeStock(EStockType.IN, stockRealTimeBean);
 
            if (!stockRealTimeBean.getPcp().contains("-")) {
                stockRealTimeBean.setPcp("+" + stringObjectMap.get("ChgPct") + "%");
            }
            if (localNotifyEnabled) {
                ObjectMapper objectMapper = new ObjectMapper();
                sendLocal(objectMapper.writeValueAsString(stockRealTimeBean));
            }
            StockRealTimeBean stockDetailBean = new Gson().fromJson(s, StockRealTimeBean.class);
            RedisKeyUtil.setCacheRealTimeStock(EStockType.IN, stockDetailBean);
        } catch (Exception e) {
            log.warn("websocket {} 消息处理失败: {}", eStockType.getCode(), e.getMessage());
        }
    }
 
    public static Map<String, String> jsonToMap(String json) {
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, String>>() {
        }.getType();
        return gson.fromJson(json, type);
    }
 
    @Override
    public void onClose(int i, String s, boolean b) {
        stopHeartbeat();
        log.info("websocket {} 连接关闭 code={} reason={} remote={}", eStockType.getCode(), i, s, b);
    }
 
    @Override
    public void onError(Exception e) {
        log.warn("websocket {} 错误: {}", eStockType.getCode(), e.getMessage());
    }
 
    /**
     * 推送本地通知服务;必须 consume 响应体,否则 HttpClient 连接池会泄漏。
     */
    public void sendLocal(String message) {
        HttpPost httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification");
        CloseableHttpResponse response = null;
        try {
            List<BasicNameValuePair> params = new ArrayList<>();
            params.add(new BasicNameValuePair("message", message));
            httpPost.setEntity(new UrlEncodedFormEntity(params, StandardCharsets.UTF_8));
            response = HTTP_CLIENT.execute(httpPost);
            EntityUtils.consume(response.getEntity());
        } catch (IOException e) {
            localNotifyEnabled = false;
            log.warn("本地通知服务不可用,已暂停推送(127.0.0.1:8001): {}", e.getMessage());
        } finally {
            if (response != null) {
                try {
                    response.close();
                } catch (IOException ignored) {
                }
            }
        }
    }
}