use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.
the class VertxDynamicGraphQLClient method webSocketHandler.
private Uni<WebSocketSubprotocolHandler> webSocketHandler() {
return webSocketHandler.updateAndGet(currentValue -> {
if (currentValue == null) {
// if we don't have a handler, create a new one
return Uni.createFrom().<WebSocketSubprotocolHandler>emitter(handlerEmitter -> {
List<String> subprotocolIds = subprotocols.stream().map(i -> i.getProtocolId()).collect(toList());
httpClient.webSocketAbs(websocketUrl, headers, WebsocketVersion.V13, subprotocolIds, result -> {
if (result.succeeded()) {
WebSocket webSocket = result.result();
WebSocketSubprotocolHandler handler = BuiltinWebsocketSubprotocolHandlers.createHandlerFor(webSocket.subProtocol(), webSocket, subscriptionInitializationTimeout, () -> {
// if the websocket disconnects, remove the handler so we can try
// connecting again with a new websocket and handler
webSocketHandler.set(null);
});
handlerEmitter.complete(handler);
log.debug("Using websocket subprotocol handler: " + handler);
} else {
handlerEmitter.fail(result.cause());
}
});
}).memoize().indefinitely();
} else {
return currentValue;
}
});
}
use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.
the class VertxTypesafeGraphQLClientProxy method webSocketHandler.
private Uni<WebSocketSubprotocolHandler> webSocketHandler() {
return webSocketHandler.updateAndGet(currentValue -> {
if (currentValue == null) {
return Uni.createFrom().<WebSocketSubprotocolHandler>emitter(handlerEmitter -> {
List<String> subprotocolIds = subprotocols.stream().map(i -> i.getProtocolId()).collect(toList());
MultiMap headers = HeadersMultiMap.headers().addAll(new HeaderBuilder(api, null, additionalHeaders).build());
httpClient.webSocketAbs(websocketUrl, headers, WebsocketVersion.V13, subprotocolIds, result -> {
if (result.succeeded()) {
WebSocket webSocket = result.result();
WebSocketSubprotocolHandler handler = BuiltinWebsocketSubprotocolHandlers.createHandlerFor(webSocket.subProtocol(), webSocket, subscriptionInitializationTimeout, () -> {
webSocketHandler.set(null);
});
handlerEmitter.complete(handler);
log.debug("Using websocket subprotocol handler: " + handler);
} else {
handlerEmitter.fail(result.cause());
}
});
}).memoize().indefinitely();
} else {
return currentValue;
}
});
}
use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.
the class VertxTypesafeGraphQLClientProxy method executeSingleResultOperationOverWebsocket.
private Uni<Object> executeSingleResultOperationOverWebsocket(MethodInvocation method, JsonObject request) {
AtomicReference<String> operationId = new AtomicReference<>();
AtomicReference<WebSocketSubprotocolHandler> handlerRef = new AtomicReference<>();
Uni<String> rawUni = Uni.createFrom().emitter(rawEmitter -> {
webSocketHandler().subscribe().with((handler) -> {
handlerRef.set(handler);
operationId.set(handler.executeUni(request, rawEmitter));
});
});
return rawUni.onCancellation().invoke(() -> {
String id = operationId.get();
log.trace("Received onCancellation on operation ID " + id);
if (id != null) {
handlerRef.get().cancelMulti(id);
} else {
log.trace("Received onCancellation on an operation that does not have an ID yet");
}
}).onItem().transform(data -> {
Object object = new ResultBuilder(method, data).read();
if (object != null) {
return object;
} else {
throw new InvalidResponseException("Couldn't find neither data nor errors in the response: " + data);
}
});
}
use of io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler in project smallrye-graphql by smallrye.
the class VertxTypesafeGraphQLClientProxy method executeSubscriptionOverWebsocket.
private Multi<Object> executeSubscriptionOverWebsocket(MethodInvocation method, JsonObject request) {
AtomicReference<String> operationId = new AtomicReference<>();
AtomicReference<WebSocketSubprotocolHandler> handlerRef = new AtomicReference<>();
Multi<String> rawMulti = Multi.createFrom().emitter(rawEmitter -> {
webSocketHandler().subscribe().with(handler -> {
handlerRef.set(handler);
operationId.set(handler.executeMulti(request, rawEmitter));
});
});
return rawMulti.onCancellation().invoke(() -> {
handlerRef.get().cancelMulti(operationId.get());
}).onItem().transform(data -> {
Object object = new ResultBuilder(method, data).read();
if (object != null) {
return object;
} else {
throw new InvalidResponseException("Couldn't find neither data nor errors in the response: " + data);
}
});
}
Aggregations