-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathWebSocketClient.java
More file actions
247 lines (204 loc) · 8.18 KB
/
Copy pathWebSocketClient.java
File metadata and controls
247 lines (204 loc) · 8.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.bitmart.websocket;
import com.bitmart.api.common.CloudException;
import com.bitmart.api.common.GlobalConst;
import com.bitmart.api.common.JsonUtils;
import com.bitmart.api.key.CloudKey;
import com.bitmart.api.key.CloudSignature;
import com.google.common.collect.ImmutableList;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.SSLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class WebSocketClient {
private static final Logger log = LoggerFactory.getLogger(WebSocketClient.class);
private EventLoopGroup group;
Channel clientChannel;
CloudKey cloudKey;
private SslContext sslContext;
private URI uri;
private String host;
private int port;
final List<String> reconnectionChannel = new ArrayList<>();
boolean reconnectionUseLogin = false;
boolean isClose = false;
boolean isSpot = true;
public WebSocketCallBack callBack;
public WebSocketClient(WebSocketCallBack callBack) throws CloudException {
init(GlobalConst.CLOUD_SPOT_WS_PUBLIC_URL, null, callBack);
}
public WebSocketClient(CloudKey cloudKey, WebSocketCallBack callBack) throws CloudException {
init(GlobalConst.CLOUD_SPOT_WS_PUBLIC_URL, cloudKey, callBack);
}
public WebSocketClient(String url, WebSocketCallBack callBack) throws CloudException {
init(url, null, callBack);
}
public WebSocketClient(String url, CloudKey cloudKey, WebSocketCallBack callBack) throws CloudException {
init(url, cloudKey, callBack);
}
private void init(String url, CloudKey cloudKey, WebSocketCallBack callBack) throws CloudException {
this.cloudKey = cloudKey;
this.callBack = callBack;
try {
this.uri = new URI(url);
} catch (URISyntaxException e) {
throw new CloudException("URISyntaxException" + e.getMessage());
}
this.host = uri.getHost() == null ? "127.0.0.1" : uri.getHost();
String scheme = uri.getScheme() == null ? "ws" : uri.getScheme();
if (uri.getPort() == -1) {
if ("ws".equalsIgnoreCase(scheme)) {
this.port = 80;
} else if ("wss".equalsIgnoreCase(scheme)) {
this.port = 443;
} else {
this.port = -1;
}
} else {
this.port = uri.getPort();
}
if (!"ws".equalsIgnoreCase(scheme) && !"wss".equalsIgnoreCase(scheme)) {
throw new CloudException("Only WS(S) is supported." + url);
}
final boolean ssl = "wss".equalsIgnoreCase(scheme);
if (ssl) {
try {
this.sslContext = SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE).build();
} catch (SSLException e) {
throw new CloudException("SSLException:" + e.getMessage());
}
}
connection();
}
private void connection() throws CloudException {
try {
if (this.group == null) {
this.group = new NioEventLoopGroup();
}
final WebSocketClientHandler handler =
new WebSocketClientHandler(
WebSocketClientHandshakerFactory.newHandshaker(
uri, WebSocketVersion.V13, null, false, new DefaultHttpHeaders()),
this);
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group)
.channel(NioSocketChannel.class)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
if (sslContext != null) {
p.addLast(sslContext.newHandler(ch.alloc(), host, port));
}
p.addLast(
new HttpClientCodec(),
new HttpObjectAggregator(8192),
handler);
}
});
this.clientChannel = bootstrap.connect(uri.getHost(), port).sync().channel();
handler.handshakeFuture().sync();
keepalive();
} catch (Exception e) {
throw new CloudException(e.getMessage());
}
}
public void reconnection() {
try {
log.info("WebSocket Client Reconnection to {}", this.uri.toString());
connection();
if (this.reconnectionUseLogin) {
this.login();
}
if (!CollectionUtils.isEmpty(this.reconnectionChannel)) {
int count = 0;
for (String channel : this.reconnectionChannel) {
this.clientChannel.writeAndFlush(new TextWebSocketFrame(channel));
count++;
if (count % 100 == 0) {
try {
Thread.sleep(2000L);
} catch (InterruptedException e) { }
}
}
}
} catch (CloudException e) {
e.printStackTrace();
}
}
public void login() throws CloudException {
this.reconnectionUseLogin = true;
CloudSignature.Signature signature = CloudSignature.create(
"bitmart.WebSocket",
this.cloudKey.getApiSecret(),
this.cloudKey.getMemo());
OpParam opParam = new OpParam().setOp("login").setArgs(ImmutableList.of(
this.cloudKey.getApiKey(),
signature.getTimestamp(),
signature.getSign()
));
String param = JsonUtils.toJson(opParam);
log.debug("WebSocket Client Send:{}", param);
this.clientChannel.writeAndFlush(new TextWebSocketFrame(param));
// Waiting for login result
try {
Thread.sleep(2000L);
} catch (InterruptedException e) { }
}
public void send(String message) {
if (!this.reconnectionChannel.contains(message)) {
this.reconnectionChannel.add(message);
}
this.clientChannel.writeAndFlush(new TextWebSocketFrame(message));
}
public void send(OpParam opParam) {
String param = JsonUtils.toJson(opParam);
if (log.isDebugEnabled()) {
log.debug("WebSocket Client Send:{}", param);
}
send(param);
}
void keepalive() {
Channel channel = this.clientChannel;
new Timer("WebSocket-Keepalive").schedule(new TimerTask() {
@Override
public void run() {
if (channel.isActive()) {
channel.writeAndFlush(new TextWebSocketFrame("ping"));
}
}
}, 2000, 10000);
}
public void stop(String reason) {
log.error("WebSocket Client Stop. reason={}", reason);
this.isClose = true;
this.clientChannel.close();
this.group.shutdownGracefully();
}
public boolean isClose() {
return this.isClose;
}
}