-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathPerMessageDeflateExample.java
82 lines (67 loc) · 2.44 KB
/
PerMessageDeflateExample.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
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import org.java_websocket.WebSocket;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.extensions.permessage_deflate.PerMessageDeflateExtension;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.handshake.ServerHandshake;
import org.java_websocket.server.WebSocketServer;
/**
* This class only serves the purpose of showing how to enable PerMessageDeflateExtension for both
* server and client sockets.<br> Extensions are required to be registered in
*
* @see Draft objects and both
* @see WebSocketClient and
* @see WebSocketServer accept a
* @see Draft object in their constructors. This example shows how to achieve it for both server and
* client sockets. Once the connection has been established, PerMessageDeflateExtension will be
* enabled and any messages (binary or text) will be compressed/decompressed automatically.<br>
* Since no additional code is required when sending or receiving messages, this example skips those
* parts.
*/
public class PerMessageDeflateExample {
private static final Draft perMessageDeflateDraft = new Draft_6455(
new PerMessageDeflateExtension());
private static final int PORT = 8887;
private static class DeflateClient extends WebSocketClient {
public DeflateClient() throws URISyntaxException {
super(new URI("ws://localhost:" + PORT), perMessageDeflateDraft);
}
@Override
public void onOpen(ServerHandshake handshakedata) {
}
@Override
public void onMessage(String message) {
}
@Override
public void onClose(int code, String reason, boolean remote) {
}
@Override
public void onError(Exception ex) {
}
}
private static class DeflateServer extends WebSocketServer {
public DeflateServer() {
super(new InetSocketAddress(PORT), Collections.singletonList(perMessageDeflateDraft));
}
@Override
public void onOpen(WebSocket conn, ClientHandshake handshake) {
}
@Override
public void onClose(WebSocket conn, int code, String reason, boolean remote) {
}
@Override
public void onMessage(WebSocket conn, String message) {
}
@Override
public void onError(WebSocket conn, Exception ex) {
}
@Override
public void onStart() {
}
}
}