1 package org.oxerr.huobi.examples.websocket;
2
3 import java.net.MalformedURLException;
4 import java.net.URI;
5 import java.time.Instant;
6 import java.util.concurrent.TimeUnit;
7
8 import org.oxerr.huobi.websocket.HuobiSocketClient;
9 import org.oxerr.huobi.websocket.dto.Percent;
10 import org.oxerr.huobi.websocket.dto.request.historydata.ReqTimeLineRequest;
11 import org.oxerr.huobi.websocket.dto.request.marketdata.Message;
12 import org.oxerr.huobi.websocket.dto.request.marketdata.PushType;
13 import org.oxerr.huobi.websocket.dto.response.Response;
14 import org.oxerr.huobi.websocket.dto.response.historydata.ReqMarketDepthResponse;
15 import org.oxerr.huobi.websocket.dto.response.marketdata.MarketDepthDiff;
16 import org.oxerr.huobi.websocket.dto.response.payload.Payload;
17 import org.oxerr.huobi.websocket.dto.response.payload.ReqMarketDepthPayload;
18 import org.oxerr.huobi.websocket.event.ResponseListener;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.slf4j.bridge.SLF4JBridgeHandler;
22
23
24
25
26 public class WebSocketDemo {
27
28 private static final Logger log = LoggerFactory.getLogger(WebSocketDemo.class);
29
30 private static volatile ReqMarketDepthPayload depth;
31
32 public static void main(String[] args) throws MalformedURLException,
33 InterruptedException {
34
35 SLF4JBridgeHandler.removeHandlersForRootLogger();
36 SLF4JBridgeHandler.install();
37
38 HuobiSocketClient client = new HuobiSocketClient(URI.create(
39 "http://hq.huobi.com:80").toURL());
40 client.addListener(new ResponseListener() {
41
42 @Override
43 public void onResponse(Response<? extends Payload> response) {
44 log.info("{}", response);
45 if (response instanceof ReqMarketDepthResponse) {
46 ReqMarketDepthResponse reqMarketDepthResponse = (ReqMarketDepthResponse) response;
47 depth = reqMarketDepthResponse.getPayload();
48 log.info("depth: {}", depth);
49 } else if (response instanceof MarketDepthDiff) {
50 MarketDepthDiff marketDepthDiff = (MarketDepthDiff) response;
51 if (depth != null) {
52 depth.merge(marketDepthDiff.getPayload());
53 log.info("merged depth: {}", depth);
54 if (marketDepthDiff.getPayload().getBidInsert().getPrice().length > 0
55 || marketDepthDiff.getPayload().getAskInsert().getPrice().length > 0) {
56 System.exit(0);
57 }
58 }
59 }
60 }
61 });
62
63 client.connect();
64
65 final String btccny = "btccny", ltccny = "ltccny";
66
67
68
69 ReqTimeLineRequest req = new ReqTimeLineRequest(1, btccny);
70 req.setFrom(Instant.ofEpochMilli(1400000000L));
71 req.setTo(Instant.ofEpochMilli(1400000220L));
72 client.send(req);
73
74
75
76
77
78
79
80
81
82
83 Message message = new Message();
84
85
86 message.addMarketDepthDiff(btccny, PushType.PUSH_LONG, Percent.PERCENT10);
87
88
89
90
91
92
93
94 TimeUnit.MINUTES.sleep(1);
95
96
97 TimeUnit.SECONDS.sleep(5);
98 client.disconnect();
99 }
100
101 }