-
Notifications
You must be signed in to change notification settings - Fork 0
/
spring_ws_client.java
76 lines (62 loc) · 2.54 KB
/
spring_ws_client.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
///usr/bin/env jbang "$0" "$@" ; exit $?
//JAVA 17+
//DEPS info.picocli:picocli:4.6.3
//DEPS org.springframework.boot:spring-boot-starter-webflux:3.0.0
import org.springframework.web.reactive.socket.WebSocketMessage;
import org.springframework.web.reactive.socket.WebSocketSession;
import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient;
import org.springframework.web.reactive.socket.client.WebSocketClient;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Parameters;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.util.Scanner;
import java.util.UUID;
import java.util.concurrent.Callable;
import static picocli.CommandLine.Option;
@Command(name = "spring_ws_client", mixinStandardHelpOptions = true, version = "spring_ws_client 0.1",
description = "ws_client made with jbang")
class spring_ws_client implements Callable<Integer> {
@Parameters(index = "0", description = "username", defaultValue = "random")
String name;
@Option(names = {"-p", "--port"}, description = "port", defaultValue = "8080")
Integer port;
@Option(names = {"-l", "--location"}, description = "host location", defaultValue = "localhost")
String host;
public static void main(String... args) {
int exitCode = new CommandLine(new spring_ws_client()).execute(args);
new Scanner(System.in).nextLine(); // Don't close immediately.
System.exit(exitCode);
}
/**
* client executes given uri and has a callback to session
* session callback first sends a hi message and then
* listens to all incoming messages
*/
@Override
public Integer call() throws Exception { // your business logic goes here...
client()
.execute(uri(), session -> session
.send(Mono.just(send(session)))
.thenMany(session.receive().map(this::mapMsg).log())
.then()
)//end execute
.subscribe();
return 0;
}
private static WebSocketMessage send(WebSocketSession session) {
return session.textMessage("hi");
}
private String mapMsg(WebSocketMessage webSocketMessage) {
return webSocketMessage.getPayloadAsText();
}
private static WebSocketClient client() {
return new ReactorNettyWebSocketClient();
}
private URI uri() {
var n = name.equals("random") ? UUID.randomUUID() : name;;
return URI.create("ws://%s:%s/stream"
.formatted(host, port));
}
}