Search in sources :

Example 1 with ApolloWSConnectionInitEvent

use of io.vertx.ext.web.handler.graphql.ApolloWSConnectionInitEvent in project vertx-web by vert-x3.

the class ApolloWSConnectionHandler method handleMessage.

private void handleMessage(JsonObject jsonObject) {
    String opId = jsonObject.getString("id");
    ApolloWSMessageType type = from(jsonObject.getString("type"));
    if (type == null) {
        sendMessage(opId, ERROR, "Unknown message type: " + jsonObject.getString("type"));
        return;
    }
    ApolloWSMessageImpl message = new ApolloWSMessageImpl(serverWebSocket, type, jsonObject);
    Handler<ApolloWSMessage> mh = apolloWSHandler.getMessageHandler();
    if (mh != null) {
        mh.handle(message);
    }
    Handler<ApolloWSConnectionInitEvent> connectionInitHandler = apolloWSHandler.getConnectionInitHandler();
    switch(type) {
        case CONNECTION_INIT:
            if (!connectionInitialized.compareAndSet(false, true)) {
                sendMessage(opId, ERROR, "CONNECTION_INIT can only be sent once").onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
                break;
            }
            if (connectionInitHandler != null) {
                connectionInitHandler.handle(new ApolloWSConnectionInitEvent() {

                    @Override
                    public ApolloWSMessage message() {
                        return message;
                    }

                    @Override
                    public boolean tryComplete(Object o) {
                        return connectionPromise.tryComplete(o);
                    }

                    @Override
                    public boolean tryFail(Throwable throwable) {
                        return connectionPromise.tryFail(throwable);
                    }

                    @Override
                    public Future<Object> future() {
                        return connectionPromise.future();
                    }
                });
            } else {
                connectionPromise.complete();
            }
            connectionPromise.future().onComplete(ar -> {
                if (ar.succeeded()) {
                    connect();
                } else {
                    sendMessage(opId, CONNECTION_ERROR, ar.cause().getMessage()).onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
                }
            });
            break;
        case CONNECTION_TERMINATE:
            serverWebSocket.close();
            break;
        case START:
            if (!connectionInitialized.get()) {
                sendMessage(opId, ERROR, "CONNECTION_INIT has to be sent before START").onComplete(v -> serverWebSocket.close(WS_INTERNAL_ERROR));
                break;
            }
            connectionPromise.future().onComplete(ar -> {
                if (ar.succeeded()) {
                    ApolloWSMessage messageWithParams = new ApolloWSMessageImpl(serverWebSocket, type, jsonObject, ar.result());
                    start(messageWithParams);
                } else {
                    sendMessage(opId, ERROR, ar.cause().getMessage());
                    stop(opId);
                }
            });
            break;
        case STOP:
            stop(opId);
            break;
        default:
            sendMessage(opId, ERROR, "Unsupported message type: " + type);
            break;
    }
}
Also used : ApolloWSConnectionInitEvent(io.vertx.ext.web.handler.graphql.ApolloWSConnectionInitEvent) ApolloWSMessageType(io.vertx.ext.web.handler.graphql.ApolloWSMessageType) ApolloWSMessage(io.vertx.ext.web.handler.graphql.ApolloWSMessage) Future(io.vertx.core.Future) JsonObject(io.vertx.core.json.JsonObject) ErrorUtil.toJsonObject(io.vertx.ext.web.handler.graphql.impl.ErrorUtil.toJsonObject)

Aggregations

Future (io.vertx.core.Future)1 JsonObject (io.vertx.core.json.JsonObject)1 ApolloWSConnectionInitEvent (io.vertx.ext.web.handler.graphql.ApolloWSConnectionInitEvent)1 ApolloWSMessage (io.vertx.ext.web.handler.graphql.ApolloWSMessage)1 ApolloWSMessageType (io.vertx.ext.web.handler.graphql.ApolloWSMessageType)1 ErrorUtil.toJsonObject (io.vertx.ext.web.handler.graphql.impl.ErrorUtil.toJsonObject)1