package com.xc.controller; import com.alibaba.fastjson.JSONObject; import com.xc.utils.websocket.WsClient; import com.xc.service.InitStockListService; import com.xc.utils.PropertiesUtil; import com.xc.vo.stock.StockVO; import org.java_websocket.WebSocket; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import java.math.BigDecimal; import java.net.URI; import java.util.ArrayList; import java.util.List; import java.util.UUID; @Controller @RequestMapping({"/ws/test/"}) public class WebSocketTestController { private static final Logger log = LoggerFactory.getLogger(WebSocketTestController.class); @Autowired private InitStockListService initStockListService; // 连接 数据解析服务器 @RequestMapping({"start.do"}) @ResponseBody public String star(String key) { try { // String url = PropertiesUtil.getProperty("ws.url")+ UUID.randomUUID().toString().replace("-",""); String url = PropertiesUtil.getProperty("ws.url")+key; WsClient.getInstance(initStockListService); WsClient myClient = new WsClient(new URI(url)); myClient.connect(); while (!myClient.getReadyState().equals(WebSocket.READYSTATE.OPEN)) { System.out.println("连接中···请稍后"); Thread.sleep(1000); } } catch (Exception e) { e.printStackTrace(); } return "true"; } @RequestMapping({"killws.do"}) @ResponseBody public String killws() { try { String url = PropertiesUtil.getProperty("ws.url")+ UUID.randomUUID().toString().replace("-",""); WsClient myClient = new WsClient(new URI(url)); myClient.closeWebSocket(); } catch (Exception e) { e.printStackTrace(); } return "true"; } @RequestMapping({"init.do"}) @ResponseBody public String init() { try { initStockListService.initStockToRedis(); } catch (Exception e) { e.printStackTrace(); } return "true"; } @RequestMapping({"test.do"}) @ResponseBody public String star2() { try { // initStockListService.initStockToRedis(); String str = "{\"sellvolume1\":7214,\"closedamount\":\"\",\"date\":\"20201214\",\"sellvolume3\":9836,\"sellvolume2\":6763,\"addTime\":\"2020-12-17 14:03:02\",\"today_max\":\"2.9\",\"tradestatus\":1,\"buyprice1\":2.8351,\"buyprice2\":2.8250999355316164,\"involume\":1000,\"sellvolume5\":7096,\"sellvolume4\":5194,\"dateThirty\":1607914800,\"buyvolume4\":9334,\"buyvolume5\":5127,\"high\":2.9,\"buyprice3\":2.8150999450683596,\"buyprice4\":2.8050999546051028,\"buyvolume1\":6580,\"averageprice\":2.82615,\"buyprice5\":2.795099964141846,\"buyvolume2\":7445,\"buyvolume3\":7364,\"id\":84,\"spell\":\"eos\",\"dateSixty\":1607914800,\"open_px\":\"2.762\",\"volume\":10090752.259622773,\"realTime\":1607913388,\"closedvolume\":\"\",\"dateFifteen\":1607913900,\"name\":\"柚子币\",\"today_min\":\"2.7523\",\"preclose\":\"12.635600\",\"highlimit\":10,\"commissiondiff\":1,\"gid\":\"eos\",\"code\":\"eos\",\"transState\":0,\"speed\":0,\"nowPrice\":2.8351,\"commissionratio\":1,\"dateFive\":1607913600,\"low\":2.7523,\"business_amount\":\"10090752.259622773\",\"now\":2.8351,\"closedtime\":\"\",\"dateOne\":1607913420,\"lowlimit\":5,\"amount\":28597935.761368953,\"change\":-9.8005,\"sellprice3\":2.8351,\"sellprice4\":2.8351,\"sellprice5\":2.8351,\"volumeratio\":1,\"sellprice1\":2.8351,\"sellprice2\":2.8351,\"outvolume\":1000,\"time\":\"103628\",\"hcrate\":\"-0.7756\",\"business_balance\":\"28597935.761368953\",\"open\":2.762,\"roundlot\":100,\"pctchange\":-0.7756}"; JSONObject parse = (JSONObject) JSONObject.parse(str); StockVO stockVO = new StockVO(); stockVO.setId(parse.getInteger("id")); stockVO.setName(parse.getString("name")); stockVO.setCode(parse.getString("code")); stockVO.setSpell(parse.getString("spell")); stockVO.setGid(parse.getString("gid")); stockVO.setNowPrice(parse.get("nowPrice")+""); stockVO.setHcrate(new BigDecimal(parse.get("hcrate")+"")); stockVO.setToday_max(parse.get("today_max")+""); stockVO.setToday_min(parse.get("today_min")+""); stockVO.setBusiness_balance(parse.get("business_balance")+""); stockVO.setBusiness_amount(parse.get("business_amount")+""); stockVO.setPreclose_px(parse.get("preclose")+""); stockVO.setOpen_px(parse.get("open")+""); stockVO.setTransState(parse.getInteger("transState")); stockVO.setDepositAmt(parse.getInteger("depositAmt")); stockVO.setMinImg(parse.getString("minImg")); stockVO.setDayImg(parse.getString("dayImg")); stockVO.setWeekImg(parse.getString("weekImg")); stockVO.setMonthImg(parse.getString("monthImg")); stockVO.setChange(parse.get("change")+""); List stockVOList = new ArrayList<>(); stockVOList.add(stockVO); initStockListService.updateListInfo(stockVOList); } catch (Exception e) { e.printStackTrace(); } return "true"; } }