View Javadoc
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   * Demonstration of {@link HuobiSocketClient}.
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  		// Bridge/route all JUL log records to the SLF4J API.
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  		// History data API
68  		// client.reqTimeLine(btccny);
69  		ReqTimeLineRequest req = new ReqTimeLineRequest(1, btccny);
70  		req.setFrom(Instant.ofEpochMilli(1400000000L));
71  		req.setTo(Instant.ofEpochMilli(1400000220L));
72  		client.send(req);
73  		// client.reqKLine(btccny, Period.KLINE_1MIN, null, null);
74  		// client.reqMarketDepthTop(btccny);
75  		// client.reqMarketDepth(btccny, Percent.PERCENT10);
76  		// client.reqTradeDetailTop(btccny, 10);
77  		// client.reqMarketDetail(btccny);
78  
79  		// Service API
80  		// client.reqSymbolList(btccny, ltccny);
81  		// client.reqSymbolDetail(btccny, ltccny);
82  
83  		Message message = new Message();
84  		// message.addLastTimeLine(btccny, PushType.PUSH_LONG);
85  		// message.addLastKLine(btccny, PushType.PUSH_LONG, Period.KLINE_1MIN);
86  		message.addMarketDepthDiff(btccny, PushType.PUSH_LONG, Percent.PERCENT10);
87  		// message.addMarketDepthTopDiff(btccny, PushType.PUSH_LONG);
88  		// message.addMarketDetail(btccny, PushType.PUSH_LONG);
89  		// message.addTradeDetail(btccny, PushType.PUSH_LONG);
90  		// message.addMarketOverview(btccny, PushType.PUSH_LONG);
91  
92  		// client.reqMsgSubscribe(message);
93  
94  		TimeUnit.MINUTES.sleep(1);
95  		// client.reqMsgUnsubscribe(message);
96  
97  		TimeUnit.SECONDS.sleep(5);
98  		client.disconnect();
99  	}
100 
101 }