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 jsonToMap(String json) { Gson gson = new Gson(); Type type = new TypeToken>(){}.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 错误"); } }