is there a Java SockJS client for Vert.x available?
Similar to the TCP/IP bridge, but based on SockJS.
Reason is that we want a unified protocol stack, connecting clients
to Vert.x. For JavaScript we can use vertx3-eventbus-client, which work great.
We are looking now for a similar solution for Java.
1
votes
1 Answers
1
votes
There isn't yet (work-in-progress). However you can write a basic client yourself using the Vert.x HttpClient:
- open a websocket
- send pings periodically to prevent the connection from being closed
- register a handler
- listen for messages
Here's an example:
client.websocket(HTTP_PORT, HTTP_HOST, "/eventbus/websocket", ws -> {
JsonObject msg = new JsonObject().put("type", "ping");
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
// Send pings periodically to avoid the websocket connection being closed
vertx.setPeriodic(5000, id -> {
JsonObject msg = new JsonObject().put("type", "ping");
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
});
// Register
JsonObject msg = new JsonObject().put("type", "register").put("address", "my-address");
ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(msg.encode(), true));
ws.handler(buff -> {
JsonObject json = new JsonObject(buff.toString()).getJsonObject("body");
// Do stuff with the body
});
});
If you need to work with different addresses then your handler will have to inspect the JSON object, not just get the body.