Vert.x 2中的WebSocket服务器(转换为Vert.x 3)?

时间:2022-04-01 18:02:35

I'm doing a client / server Websocket. The client is in JavaScript I already have it and the Server is in Vert.x (Java), which is where I have the doubts, because I found a code in Vert.x 2 and I would like someone to help me convert it to Vert .x 3

我正在做一个客户端/服务器Websocket。客户端是用JavaScript我已经拥有它而服务器在Vert.x(Java)中,这是我有疑虑的地方,因为我在Vert.x 2中找到了一个代码,我想有人帮我转换为Vert .x 3

public class WebserverVerticle extends Verticle {

@Override
public void start() {
final Pattern chatUrlPattern = Pattern.compile("/chat/(\\w+)");
final EventBus eventBus = vertx.eventBus();

// 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
//   Aquí vendría el código de la segunda parte
     ....................
//   Lo paso luego
}

} I have a little translated but I do not know very well the part of the .get (". * . (Css | js)") ...... I have the following:

我有一点翻译,但我不太了解.get的部分(“。*。(Css | js)”)......我有以下内容:

public class WebserverVerticle extends AbstractVerticle{

public void start(Future<Void> startFuture) {
Router router = Router.router(vertx);

Pattern chatUrlPattern = Pattern.compile("/chat/(\\w+)");
EventBus eventBus = vertx.eventBus();

//HTTP Server 
router.route("/").handler(routingContext -> {
    routingContext.request().response().sendFile("index2.html");
}).handler(routingContext -> {
    routingContext.request().response().sendFile("web/" + new File(routingContext.request().path()));
});

vertx.createHttpServer().requestHandler(router::accept).listen(8080, "localhost");


//Websocket Chat Server
}

I do not know if it is right what I have translated and have if you can help me turn it into Vert.x 3.

我不知道我翻译的是否正确,如果你可以帮助我把它变成Vert.x 3。

1 个解决方案

#1


0  

What you're looking for called StaticHandler.

你在寻找什么称为StaticHandler。

router.route("/web/*").handler(StaticHandler.create());

#1


0  

What you're looking for called StaticHandler.

你在寻找什么称为StaticHandler。

router.route("/web/*").handler(StaticHandler.create());