-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathWsEchoClient.java
More file actions
46 lines (37 loc) · 1.47 KB
/
WsEchoClient.java
File metadata and controls
46 lines (37 loc) · 1.47 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
import jakarta.websocket.ContainerProvider;
import jakarta.websocket.Endpoint;
import jakarta.websocket.EndpointConfig;
import jakarta.websocket.MessageHandler;
import jakarta.websocket.Session;
import jakarta.websocket.WebSocketContainer;
import java.io.IOException;
import java.net.URI;
import java.util.Scanner;
public class WsEchoClient extends Endpoint {
public Session session;
public static void main(String[] args) throws Exception {
WsEchoClient client = new WsEchoClient();
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a message you want to echo:");
while(true) {
client.send(scanner.nextLine());
}
}
public WsEchoClient() throws Exception {
URI uri = new URI("ws://localhost:8080/ws");
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
session = container.connectToServer(this, uri);
this.session.addMessageHandler(new MessageHandler.Whole<String>() {
public void onMessage(String message) {
System.out.println(message);
System.out.println("\nEnter another message you want to echo:");
}
});
}
public void send(String message) throws IOException {
session.getBasicRemote().sendText(message);
}
// This method must be overridden, but we don't have to do anything with it
public void onOpen(Session session, EndpointConfig endpointConfig) {
}
}