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;
}
}
Aggregations