-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathHttpClientWebSocket.java
112 lines (86 loc) · 2.94 KB
/
HttpClientWebSocket.java
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
package com.xbox.httpclient;
import java.nio.ByteBuffer;
import java.util.concurrent.TimeUnit;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
public final class HttpClientWebSocket extends WebSocketListener {
private final static OkHttpClient OK_CLIENT;
static {
OK_CLIENT = new OkHttpClient();
}
HttpClientWebSocket(long owner) {
this.headers = new Headers.Builder();
this.owner = owner;
this.pingInterval = 0;
}
public void setPingInterval(long pingInterval) {
this.pingInterval = pingInterval;
}
public void addHeader(String name, String value) {
headers.add(name, value);
}
public void connect(String url, String subProtocol) {
addHeader("Sec-WebSocket-Protocol", subProtocol);
Request request = new Request.Builder()
.url(url)
.headers(headers.build())
.build();
OkHttpClient clientWithPing = OK_CLIENT.newBuilder()
.pingInterval(pingInterval, TimeUnit.SECONDS) // default is 0, which disables pings
.build();
socket = clientWithPing.newWebSocket(request, this);
}
public boolean sendMessage(String message) {
return socket.send(message);
}
public boolean sendBinaryMessage(java.nio.ByteBuffer message) {
return socket.send(okio.ByteString.of(message));
}
public void disconnect(int closeStatus) {
socket.close(closeStatus, null);
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
onOpen();
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
onFailure(response != null ? response.code() : -1);
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {}
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
onClose(code);
}
@Override
public void onMessage(WebSocket webSocket, String text) {
onMessage(text);
}
@Override
public void onMessage(WebSocket webSocket, okio.ByteString bytes) {
// These needs to be a directly allocated ByteBuffer or
// native layer won't be able to access this.
ByteBuffer buffer = ByteBuffer.allocateDirect(bytes.size());
buffer.put(bytes.toByteArray());
buffer.position(0);
onBinaryMessage(buffer);
}
public native void onOpen();
public native void onFailure(int statusCode);
public native void onClose(int code);
public native void onMessage(String text);
public native void onBinaryMessage(ByteBuffer data);
protected void finalize()
{
socket.cancel();
}
private final Headers.Builder headers;
private final long owner;
private long pingInterval;
private WebSocket socket;
}