1 package org.oxerr.okcoin.examples.websocket;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.concurrent.TimeUnit;
9
10 import javax.websocket.ContainerProvider;
11 import javax.websocket.DeploymentException;
12 import javax.websocket.Session;
13 import javax.websocket.WebSocketContainer;
14
15 import org.oxerr.okcoin.rest.OKCoinDigest;
16 import org.oxerr.okcoin.websocket.OKCoinClientEndpoint;
17 import org.oxerr.okcoin.websocket.event.OKCoinDataListener;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class Client {
22
23 private static final Logger log = LoggerFactory.getLogger(Client.class);
24
25 public static void main(String[] args) throws DeploymentException,
26 IOException, URISyntaxException, InterruptedException {
27 String apiKey = args.length > 0 ? args[0] : null, secretKey = args.length > 1 ? args[1] : null;
28
29 OKCoinClientEndpoint endpoint = new OKCoinClientEndpoint();
30
31 OKCoinDataListener printLogListener = new OKCoinDataListener() {
32
33 @Override
34 public void onMessage(Session session, Object data) {
35 log.info("data: {}", data);
36 }
37 };
38
39 endpoint.addChannelListener("ok_btccny_ticker", printLogListener);
40 endpoint.addChannelListener("ok_btccny_depth", printLogListener);
41 endpoint.addChannelListener("ok_btccny_depth60", printLogListener);
42 endpoint.addChannelListener("ok_btccny_trades_v1", printLogListener);
43 endpoint.addChannelListener("ok_btccny_kline_1min", printLogListener);
44
45 endpoint.addChannelListener("ok_cny_realtrades", printLogListener);
46 endpoint.addChannelListener("ok_spotcny_trade", printLogListener);
47 endpoint.addChannelListener("ok_spotcny_cancel_order", printLogListener);
48 endpoint.addChannelListener("ok_spotcny_userinfo", printLogListener);
49 endpoint.addChannelListener("ok_spotcny_order_info", printLogListener);
50
51 WebSocketContainer container = ContainerProvider
52 .getWebSocketContainer();
53 Session session = container.connectToServer(endpoint, new URI(
54 "wss://real.okcoin.cn:10440/websocket/okcoinapi"));
55
56 endpoint.addChannel(session, "ok_btccny_ticker");
57 endpoint.addChannel(session, "ok_btccny_depth");
58 endpoint.addChannel(session, "ok_btccny_depth60");
59 endpoint.addChannel(session, "ok_btccny_trades_v1");
60 endpoint.addChannel(session, "ok_btccny_kline_1min");
61
62 if (apiKey != null && secretKey != null) {
63 Map<String, String> parameters = new HashMap<>();
64 parameters.put("api_key", apiKey);
65 parameters.put("sign", new OKCoinDigest(secretKey).digestParams(parameters));
66 endpoint.addChannel(session, "ok_cny_realtrades", parameters);
67
68 parameters = new HashMap<>();
69 parameters.put("api_key", apiKey);
70 parameters.put("symbol", "btc_cny");
71 parameters.put("type", "buy");
72 parameters.put("price", "50");
73 parameters.put("amount", "0.02");
74 parameters.put("sign", new OKCoinDigest(secretKey).digestParams(parameters));
75
76
77 parameters = new HashMap<>();
78 parameters.put("api_key", apiKey);
79 parameters.put("symbol", "btc_cny");
80 parameters.put("order_id", "1");
81 parameters.put("sign", new OKCoinDigest(secretKey).digestParams(parameters));
82
83
84 parameters = new HashMap<>();
85 parameters.put("api_key", apiKey);
86 parameters.put("sign", new OKCoinDigest(secretKey).digestParams(parameters));
87 endpoint.addChannel(session, "ok_spotcny_userinfo", parameters);;
88
89 parameters = new HashMap<>();
90 parameters.put("api_key", apiKey);
91 parameters.put("symbol", "btc_cny");
92 parameters.put("order_id", "1");
93 parameters.put("sign", new OKCoinDigest(secretKey).digestParams(parameters));
94
95 }
96
97 TimeUnit.MINUTES.sleep(5);
98 }
99
100 }