forked from vert-x3/vertx-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubscriptionClient.java
40 lines (33 loc) · 1.27 KB
/
SubscriptionClient.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
package io.vertx.example.web.graphql;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Launcher;
import io.vertx.core.http.HttpClient;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.http.WebSocket;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.handler.graphql.ApolloWSMessageType;
public class SubscriptionClient extends AbstractVerticle {
public static void main(String[] args) {
Launcher.executeCommand("run", SubscriptionClient.class.getName());
}
@Override
public void start() {
HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
httpClient.webSocket("/graphql", websocketRes -> {
if (websocketRes.succeeded()) {
WebSocket webSocket = websocketRes.result();
webSocket.handler(message -> {
System.out.println(message.toJsonObject().encodePrettily());
});
JsonObject request = new JsonObject()
.put("id", "1")
.put("type", ApolloWSMessageType.START.getText())
.put("payload", new JsonObject()
.put("query", "subscription { links { url, postedBy { name } } }"));
webSocket.write(request.toBuffer());
} else {
websocketRes.cause().printStackTrace();
}
});
}
}