-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
App.java
76 lines (59 loc) · 2.4 KB
/
App.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
package nl.altindag.ssl.ws;
import nl.altindag.ssl.SSLFactory;
import nl.altindag.ssl.util.CertificateUtils;
import nl.altindag.ssl.util.JettySslUtils;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketListener;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import java.net.URI;
import java.util.concurrent.TimeUnit;
public class App {
public static void main(String[] args) throws Exception {
var sslFactory = SSLFactory.builder()
.withTrustMaterial(CertificateUtils.loadCertificate("isrg-root-x1.pem"))
.build();
var sslContextFactory = JettySslUtils.forClient(sslFactory);
var httpClient = new HttpClient(sslContextFactory);
var webSocketClient = new WebSocketClient(httpClient);
webSocketClient.start();
MyWebSocketListener webSocketListener = new MyWebSocketListener();
var session = webSocketClient.connect(webSocketListener, new URI("ws://echo.websocket.org")).get();
session.getRemote().sendString("Hello there!");
// waiting till the server response before closing the client
int counter = 0;
while (!webSocketListener.hasReceivedServerResponse() && counter <= 20) {
counter++;
TimeUnit.MILLISECONDS.sleep(100);
}
webSocketClient.stop();
httpClient.stop();
}
private static class MyWebSocketListener implements WebSocketListener {
private boolean hasReceivedServerResponse = false;
@Override
public void onWebSocketBinary(byte[] bytes, int i, int i1) {
System.out.println();
}
@Override
public void onWebSocketText(String response) {
hasReceivedServerResponse = true;
System.out.println("Received the following message from the server: " + response);
}
@Override
public void onWebSocketClose(int i, String s) {
System.out.println("closed");
}
@Override
public void onWebSocketConnect(Session session) {
System.out.println("connected");
}
@Override
public void onWebSocketError(Throwable throwable) {
System.err.println("got error");
}
public boolean hasReceivedServerResponse() {
return hasReceivedServerResponse;
}
}
}