|
| 1 | +package io.vertx.example.web.graphql; |
| 2 | + |
| 3 | +import io.vertx.core.AbstractVerticle; |
| 4 | +import io.vertx.core.Launcher; |
| 5 | +import io.vertx.core.http.HttpClient; |
| 6 | +import io.vertx.core.http.HttpClientOptions; |
| 7 | +import io.vertx.core.http.WebSocket; |
| 8 | +import io.vertx.core.json.JsonObject; |
| 9 | +import io.vertx.ext.web.handler.graphql.ApolloWSMessageType; |
| 10 | + |
| 11 | +public class SubscriptionClient extends AbstractVerticle { |
| 12 | + |
| 13 | + public static void main(String[] args) { |
| 14 | + Launcher.executeCommand("run", SubscriptionClient.class.getName()); |
| 15 | + } |
| 16 | + |
| 17 | + @Override |
| 18 | + public void start() { |
| 19 | + HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080)); |
| 20 | + httpClient.webSocket("/graphql").setHandler(websocketRes -> { |
| 21 | + if (websocketRes.succeeded()) { |
| 22 | + WebSocket webSocket = websocketRes.result(); |
| 23 | + |
| 24 | + webSocket.handler(message -> { |
| 25 | + System.out.println(message.toJsonObject().encodePrettily()); |
| 26 | + }); |
| 27 | + |
| 28 | + JsonObject request = new JsonObject() |
| 29 | + .put("id", "1") |
| 30 | + .put("type", ApolloWSMessageType.START.getText()) |
| 31 | + .put("payload", new JsonObject() |
| 32 | + .put("query", "subscription { links { url, postedBy { name } } }")); |
| 33 | + webSocket.write(request.toBuffer()); |
| 34 | + } else { |
| 35 | + websocketRes.cause().printStackTrace(); |
| 36 | + } |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | +} |
0 commit comments