1
zj
2024-06-30 3134ce0036412b382b4ffacee73a1e2f7ba298bb
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
package com.nq.ws;
 
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nq.enums.EStockType;
import com.nq.pojo.StockRealTimeBean;
import com.nq.service.IMandatoryLiquidationService;
import com.nq.service.impl.MandatoryLiquidationService;
import com.nq.utils.ApplicationContextRegisterUtil;
import com.nq.utils.redis.RedisKeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import org.springframework.context.ApplicationContext;
 
import java.lang.reflect.Type;
import java.net.URI;
import java.util.Map;
 
@Slf4j
public class WebsocketRunClient  extends WebSocketClient {
 
    private EStockType eStockType;
    public WebsocketRunClient(URI serverUri,
    EStockType eStockType
    ) {
        super(serverUri);
        this.eStockType = eStockType;
    }
 
    @Override
    public void onOpen(ServerHandshake serverHandshake) {
        send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes());
    }
 
    @Override
    public void onMessage(String s) {
        try {
            ApplicationContext act = ApplicationContextRegisterUtil.getApplicationContext();
            MandatoryLiquidationService liquidationService = (MandatoryLiquidationService) act.getBean(IMandatoryLiquidationService.class);
            StockRealTimeBean stockDetailBean =  new Gson().fromJson(s, StockRealTimeBean.class);
            liquidationService.RealTimeDataProcess(eStockType,stockDetailBean);
        }catch (Exception e){
 
        }
    }
 
    public static Map<String, Object> jsonToMap(String json) {
        Gson gson = new Gson();
        Type type = new TypeToken<Map<String, Object>>(){}.getType();
        return gson.fromJson(json, type);
    }
 
    @Override
    public void onClose(int i, String s, boolean b) {
        log.info("websocket  印度股票  关闭"+1);
    }
 
    @Override
    public void onError(Exception e) {
        log.info("websocket 错误");
    }
}