Search code examples
javawebsocketvert.xstomp

How to deal with StompServerHandlers in vert-x


I'm using vert-x 3.5.0 and I want to use StompServer as a WebSocket server

My java code is following:

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServerOptions;
import io.vertx.core.logging.Logger;
import io.vertx.core.logging.LoggerFactory;
import io.vertx.ext.stomp.StompServer;
import io.vertx.ext.stomp.StompServerHandler;
import io.vertx.ext.stomp.StompServerOptions;

StompServer server = StompServer.create(vertx, new StompServerOptions()
                .setPort(-1)
                .setWebsocketBridge(true)
                .setWebsocketPath("/stomp"))
                .handler(StompServerHandler.create(vertx)
                .receivedFrameHandler(event -> {
                    LOG.info(String.format("frame is %s", event.frame().getBodyAsString()));
                })
                .beginHandler(event -> {
                    LOG.info("Begin event");
                })
                .connectHandler(event -> {
                    LOG.info("Connect event");
                })
                .subscribeHandler(event -> {
                    LOG.info("Subscribe event");
                }));

        vertx.createHttpServer(
                new HttpServerOptions().setWebsocketSubProtocols("v10.stomp, v11.stomp, v13.stomp")
        )
                .websocketHandler(server.webSocketHandler())
                .listen(8080);

Server is running and can receive a handshake request and a message. I see it from websocket client and from extended server log

Server received request: /stomp 
[id: 0x1f899ae3, L:/127.0.0.1:8080 - R:/127.0.0.1:42550] WebSocket version V13 server handshake 
WebSocket version 13 server handshake key: Qe8G85Aw1PwuQbWKPyJSrg==, response: VQVnKr2Jp8RuK/vltKb7XqBiMTU= 
Decoding WebSocket Frame opCode=1 
Decoding WebSocket Frame length=7 

But declared handlers (receivedFrameHandler, beginHandler, connectHandler, subscribeHandler) are not working. How to deal with them?


Solution

  • You are configuring the WebSocket Stomp bridge that allows a client using web socket to interact with the Stomp server. However, this client must send STOMP frames (and expect STOMP frames).

    Typically:

    The connectEvent is called when a STOMP client connects to the server The beginEvent is called when you start a STOMP transaction.

    If your web socket client is not sending the frames (CONNECT, BEGIN...), the callbacks won't be called.

    You can see an example of the CONNECT frame here.