Route SockJS connection with variable url?

Let's say I have a bunch of clients that have their own numeric IDs. Each one connects to my server via SockJS, with something like:

var sock = new SockJS("localhost:8080/sock/100");

      

In this case, 100 is a numeric client ID, but it can be any number with any number of digits. How do I set up a SockJS router in my server-side code that allows the client to set up a SockJS connection via a URL that depends on what the user ID is? Here's a simplified version of what I have on the server side right now:

public void start() {
    HttpServer server = vertx.createHttpServer();
    SockJSHandler sockHandler = SockJSHandler.create(vertx);
    router.route("/sock/*").handler(sockHandler);
    server.requestHandler(router::accept).listen(8080);
}

      

This works fine if the client connects via localhost: 8080 / sock, but it doesn't work if I add "/ 100" to the end of the url. Instead of getting the default "Welcome to SockJS!" message, I just get "Not Found". I tried to set a path regex and I got an error stating that sub-routers cannot use pattern urls. So, is there a way to allow the client to connect via a variable url, be it / sock / 100, / sock / 15, or / sock / 1123123?

Ideally I should be able to grab the numeric ID that the client is using (for example with routing REST API calls, when you can add "/: ID" to the routing path and then grab the value the client is using), but I can't find anything. which works for SockJS connections.

Since it seems that SockJS connections are considered the same as sub-routers, and sub-routers cannot have template urls, is there any work around for this? Or is it impossible?

Edit

Just to add to the above, I've tried a couple of different things that haven't worked yet.

I tried to set up an initial main router, which is then redirected to a SockJS handler. Here was my idea:

router.routeWithRegex("/sock/\\d+").handler(context -> {
    context.reroute("/final");
});

router.route("/final").handler(SockJSHandler.create(vertx));

      

That being said, if I access localhost: 8080 / sock / 100 right through the browser, it takes me to "Welcome to SockJS!" and the Chrome Network tab shows the websockets connection was created when I tested it through my client.

However, I still get the error because the websocket is showing a status code of 200 and not 101, and I'm not 100% sure why this is happening, but I would guess it has something to do with the response that the initial handler is called. If I try to set the status code of the initial handler to 101, I still get an error because then the original handler fails.

If there is some way to get around these status codes (it looks like the websocket is expecting 101, but the first handler is expecting 200 and I think I can only choose one) then this could potentially solve that. Any ideas?

+3


source to share





All Articles