簡體   English   中英

Vert.x WebSocket返回200而不是101

[英]Vert.x WebSocket returning 200 instead of 101

我正在嘗試設置基本的HTTP和WebSocket Vert.x服務器,但是WebSocket路由始終返回200而不是101。

public class PhilTheServer extends AbstractVerticle {

    private final static int PORT = 8080;

    @Override
    public void start(Future<Void> fut) {
        // Create a router object.
        Router router = Router.router(vertx);

        // We need cookies, sessions and request bodies
        router.route().handler(CookieHandler.create());
        router.route().handler(BodyHandler.create());

        router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx)));

        // Simple auth service which uses a properties file for user/role info
        AuthProvider authProvider = ShiroAuth.create(vertx, new ShiroAuthOptions()
                .setType(ShiroAuthRealmType.PROPERTIES)
                .setConfig(new JsonObject()
                    .put("properties_path", "src/main/resources/vertx-users.properties")));

        // We need a user session handler too to make sure the user is stored in the session between requests
        router.route().handler(UserSessionHandler.create(authProvider));

        // Serve the static private pages from directory "webroot"
        router.route("/static/*").handler(StaticHandler.create("webroot/static").setCachingEnabled(false));

        // Public Index Page
        router.get("/").handler(ctx -> {
            ctx.response().sendFile("webroot/index.html");
        });

        router.mountSubRouter("/api", APIRoutes.get(vertx, authProvider));

        // Default non-handled requests:
        router.route().handler(ctx -> {
            ctx.fail(404);
        });

        // WEBSOCKET:

        BridgeOptions opts = new BridgeOptions()
            .addInboundPermitted(new PermittedOptions().setAddressRegex("*"))
            .addOutboundPermitted(new PermittedOptions().setAddressRegex("*"));

        // Create the event bus bridge and add it to the router.
        SockJSHandler ebHandler = SockJSHandler.create(vertx).bridge(opts, event -> {
            if (event.type() == BridgeEventType.SOCKET_CREATED) {
                System.out.println("A socket was created");
            } else {
                System.out.println(event.type());
            }

            event.complete(true);
        });

        router.route("/eventbus/*").handler(ebHandler);

        // Create the HTTP server and pass the "accept" method to the request handler.
        vertx
            .createHttpServer()
            .requestHandler(router::accept)
            .listen(
                    // Retrieve the port from the configuration,
                    // default to 8080.
                    config().getInteger("http.port", 8080),
                    result -> {
                        if (result.succeeded()) {
                            fut.complete();
                        } else {
                            fut.fail(result.cause());
                        }
                    }
            );
    }
}

我沒有添加SockJSHanlder ,而是嘗試在HttpServer上使用websocketHandler ,並且工作正常,但我想采用其他方法。

我的測試代碼:

HttpClient httpClient = client.websocket(PORT, "localhost", "/eventbus", headers, ws -> {
    ws.handler(buffer -> {
        JsonObject message = new JsonObject(buffer.toString());

        assertTrue(message.containsKey("type"));

        ws.close();

        async.complete();
    });

    ws.write(Buffer.buffer("{ \"type\": \"test\"}"));
});

您應該移動處理程序:

router.route("/eventbus/*").handler(ebHandler);

在您的列表中較高。 問題是您擁有將首先執行的全局處理程序,並且將不允許該處理程序處理升級到sockjs的協議。 這些處理程序是:

router.route().handler(CookieHandler.create());
router.route().handler(BodyHandler.create());
router.route().handler(SessionHandler....);
router.route().handler(ctx -> {
  ctx.fail(404);
});

由於將在任何路徑下調用router.route()因此它們將干擾協議升級。 如果將sockjs處理程序移到它們之前,則應首先執行它,並且代碼應該可以工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM