Translation of Vert.x 2 to Vert.x 3 !!

29 views
Skip to first unread message

davidr...@gmail.com

unread,
Apr 27, 2017, 12:50:15 PM4/27/17
to vert.x

Hi people,

can anyone help me pass the following code from Vert.x 2 to Vert.x 3? Is that I'm doing a client / server websockets


public class WebserverVerticle extends Verticle {
 
@Override
public void start() {
final Pattern chatUrlPattern = Pattern.compile("/chat/(\\w+)");
final EventBus eventBus = vertx.eventBus();
final Logger logger = container.logger();
 
// 1) HTTP Server
RouteMatcher httpRouteMatcher = new RouteMatcher().get("/", new
Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest request) {
request.response().sendFile("web/chat.html");
}
}).get(".*\\.(css|js)$", new Handler<HttpServerRequest>() {
@Override
public void handle(final HttpServerRequest request) {
request.response().sendFile("web/" + new File(request.path()));
}
});
 
vertx.createHttpServer().requestHandler(httpRouteMatcher).listen(8080, "localhost");


// 2) Websockets Chat Server
vertx.createHttpServer().websocketHandler(new Handler<ServerWebSocket>() {
@Override
public void handle(final ServerWebSocket ws) {
final Matcher m = chatUrlPattern.matcher(ws.path());
if (!m.matches()) {
ws.reject();
return;
}
 
final String chatRoom = m.group(1);
final String id = ws.textHandlerID();
logger.info("registering new connection with id: " + id + " for chat-room: " + chatRoom);
vertx.sharedData().getSet("chat.room." + chatRoom).add(id);
 
ws.closeHandler(new Handler<Void>() {
@Override
public void handle(final Void event) {
logger.info("un-registering connection with id: " + id + " from chat-room: " + chatRoom);
vertx.sharedData().getSet("chat.room." + chatRoom).remove(id);
}
});
 
ws.dataHandler(new Handler<Buffer>() {
@Override
public void handle(final Buffer data) {
 
ObjectMapper m = new ObjectMapper();
try {
JsonNode rootNode = m.readTree(data.toString());
((ObjectNode) rootNode).put("received", new Date().toString());
String jsonOutput = m.writeValueAsString(rootNode);
logger.info("json generated: " + jsonOutput);
for (Object chatter : vertx.sharedData().getSet("chat.room." + chatRoom)) {
eventBus.send((String) chatter, jsonOutput);
}
} catch (IOException e) {
ws.reject();
}
}
});
 
}
}).listen(8090);
}
}

Tim Fox

unread,
Apr 27, 2017, 1:41:47 PM4/27/17
to vert.x
A good place to start would be the docs for vertx-web.

RouteMatcher in Vert.x 2 has been replaced by a much more sophisticated matching mechanism there.
Reply all
Reply to author
Forward
0 new messages