| | |
| | | static { |
| | | httpPost = new HttpPost("http://127.0.0.1:8001/api/sendNotification"); // 初始化 HttpPost |
| | | } |
| | | |
| | | private Timer heartbeatTimer; |
| | | @Override |
| | | public void onOpen(ServerHandshake serverHandshake) { |
| | | send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes()); |
| | | // Timer heartbeatTimer; |
| | | // // 启动心跳定时器 |
| | | // heartbeatTimer = new Timer(); |
| | | // heartbeatTimer.schedule(new TimerTask() { |
| | | // @Override |
| | | // public void run() { |
| | | // send(("key:"+ eStockType.getStockKey()+":"+eStockType.getContryId()).getBytes()); |
| | | // } |
| | | // }, 0, 3000); // 每3秒发送一次心跳消息 |
| | | // 启动心跳定时器 |
| | | heartbeatTimer = new Timer(); |
| | | heartbeatTimer.schedule(new TimerTask() { |
| | | @Override |
| | | public void run() { |
| | | send("heartbeat"); |
| | | } |
| | | }, 0, 3000); // 每3秒发送一次心跳消息 |
| | | } |
| | | |
| | | @Override |
| | | public void onMessage(String s) { |
| | | if(!s.equals("pong") && !s.equals("身份验证成功")){ |
| | | if(!s.equals("pong") && !s.equals("身份验证成功") && !s.equals("请先身份校验")){ |
| | | Map<String, String> stringObjectMap = jsonToMap(s); |
| | | StockRealTimeBean stockRealTimeBean = new StockRealTimeBean(); |
| | | stockRealTimeBean.setPid(stringObjectMap.get("Id").toString()); |
| | |
| | | stockRealTimeBean.setPc(stringObjectMap.get("Chg").toString()); |
| | | stockRealTimeBean.setPcp(stringObjectMap.get("ChgPct").toString()+"%"); |
| | | stockRealTimeBean.setTime(stringObjectMap.get("Time").toString()); |
| | | RedisKeyUtil.setCacheRealTimeStock(EStockType.IN,stockRealTimeBean); |
| | | RedisKeyUtil.setCacheRealTimeStock(EStockType.ST,stockRealTimeBean); |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | try { |
| | | if(!stockRealTimeBean.getPcp().contains("-")){ |
| | | stockRealTimeBean.setPcp("+"+stringObjectMap.get("ChgPct").toString()+"%"); |
| | | } |
| | | String json = objectMapper.writeValueAsString(stockRealTimeBean); |
| | | sendLoca(json); |
| | | // sendLoca(json); |
| | | StockRealTimeBean stockDetailBean = new Gson().fromJson(s, StockRealTimeBean.class); |
| | | RedisKeyUtil.setCacheRealTimeStock(EStockType.ST,stockDetailBean); |
| | | } catch (JsonProcessingException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void onClose(int i, String s, boolean b) { |
| | | log.info("websocket 印度股票 关闭"+1); |
| | | public void onClose(int code, String reason, boolean remote) { |
| | | System.out.println("连接关闭: " + reason); |
| | | if (heartbeatTimer != null) { |
| | | heartbeatTimer.cancel(); // 取消心跳定时器 |
| | | } |
| | | // 尝试重新连接 |
| | | try { |
| | | System.out.println("尝试重新连接..."); |
| | | Thread.sleep(10000); // 10秒后尝试重新连接 |
| | | connect(); // 重新连接 |
| | | } catch (InterruptedException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onError(Exception e) { |
| | | log.info("websocket 错误"); |
| | | log.info("websocket 错误"+e.getMessage()); |
| | | } |
| | | |
| | | public void sendLoca(String message) { |