forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
52 lines (40 loc) · 1.74 KB
/
Main.java
File metadata and controls
52 lines (40 loc) · 1.74 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
package modern.challenge;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.WebSocket;
import java.net.http.WebSocket.Listener;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class Main {
public static void main(String[] args)
throws InterruptedException, ExecutionException, TimeoutException {
Listener wsListener = new Listener() {
@Override
public CompletionStage<?> onText(WebSocket webSocket,
CharSequence data, boolean last) {
System.out.println("Received data: " + data);
return Listener.super.onText(webSocket, data, last);
}
@Override
public void onOpen(WebSocket webSocket) {
System.out.println("Connection is open ...");
Listener.super.onOpen(webSocket);
}
@Override
public CompletionStage<?> onClose(WebSocket webSocket, int statusCode,
String reason) {
System.out.println("Closing connection: " + statusCode + " " + reason);
return Listener.super.onClose(webSocket, statusCode, reason);
}
};
HttpClient client = HttpClient.newHttpClient();
WebSocket webSocket = client.newWebSocketBuilder()
.buildAsync(URI.create("ws://stream.meetup.com/2/rsvps"), wsListener)
.get(10, TimeUnit.SECONDS);
// webSocket.sendText("I am an Meetup RSVP fan", true);
TimeUnit.SECONDS.sleep(10);
webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "ok");
}
}