Example usage for io.vertx.core.http HttpServer listen

List of usage examples for io.vertx.core.http HttpServer listen

Introduction

In this page you can find the example usage for io.vertx.core.http HttpServer listen.

Prototype

@Fluent
HttpServer listen(int port, Handler<AsyncResult<HttpServer>> listenHandler);

Source Link

Document

Like #listen(int) but supplying a handler that will be called when the server is actually listening (or has failed).

Usage

From source file:com.github.ithildir.airbot.AirBotVerticle.java

License:Open Source License

private Future<HttpServer> _startHttpServer(JsonObject configJsonObject) {
    Future<HttpServer> future = Future.future();

    HttpServer httpServer = vertx.createHttpServer();

    Router router = Router.router(vertx);

    Route authHandlerRoute = router.route();

    String username = configJsonObject.getString(ConfigKeys.USERNAME);
    String password = configJsonObject.getString(ConfigKeys.PASSWORD);

    AuthProvider authProvider = new SingleUserAuthProvider(username, password);

    authHandlerRoute.handler(BasicAuthHandler.create(authProvider));

    Route bodyHandlerRoute = router.route();

    bodyHandlerRoute.handler(BodyHandler.create());

    _addHttpRouteApiAi(router);//from w w w .ja  va 2  s  .  co m

    httpServer.requestHandler(router::accept);

    int port = configJsonObject.getInteger(ConfigKeys.PORT, _DEFAULT_PORT);

    httpServer.listen(port, future);

    return future;
}

From source file:com.github.ithildir.numbers.game.NumbersGameVerticle.java

License:Open Source License

@Override
public void start(Future<Void> startFuture) throws Exception {
    HttpServer httpServer = vertx.createHttpServer();

    Router router = Router.router(vertx);

    Route route = router.route();/*from  w w  w. j  av  a 2  s .com*/

    route.handler(BodyHandler.create());

    route = router.route(HttpMethod.POST, "/actions");

    route.handler(this::_handle);

    httpServer.requestHandler(router::accept);

    httpServer.listen(_PORT, result -> {
        if (result.succeeded()) {
            startFuture.complete();
        } else {
            startFuture.fail(result.cause());
        }
    });
}

From source file:examples.HTTPExamples.java

License:Open Source License

public void example4(Vertx vertx) {

    HttpServer server = vertx.createHttpServer();
    server.listen(8080, "myhost.com");
}

From source file:home.learning.learnexamples.components.WebComponent.java

@Override
public void start(Future<Void> fut) {
    HttpServer server = vertx.createHttpServer();

    server.requestHandler(r -> {/*from  w  ww. jav  a2 s  .c  o  m*/
        // The lambda expression here the r is an instance of HTTPServerRequest
        String uri = r.absoluteURI();
        System.out.println("The absolute URI is " + uri);
        r.response().end("<h1>WebComponent verticle running in a" + " Vert.x 3 application</h1>");
    });

    server.listen(8080, result -> {
        if (result.succeeded()) {
            System.out.println("--- Start up complete and running ---");
            fut.complete();
        } else {
            fut.fail(result.cause());
        }
    });
}

From source file:io.sqp.proxy.ServerVerticle.java

License:Open Source License

@Override
public void start() {
    // TODO: set subprotocols

    JsonObject config = config();/*from ww w .j  a  v a  2s. co  m*/

    String path = config.getString("path", DEFAULT_PATH);
    int port = config.getInteger("port", DEFAULT_PORT);
    int poolSize = config.getInteger("connectionPoolSize", DEFAULT_POOL_SIZE);
    JsonArray backendConfs = config.getJsonArray("backends");
    _executorService = Executors.newFixedThreadPool(10); // TODO: set this reasonably

    // Initialize the backend connection pool
    BackendConnectionPool connectionPool = new VertxBackendConnectionPool(vertx, poolSize, backendConfs);

    try {
        connectionPool.init();
    } catch (ServerErrorException e) {
        _logger.log(Level.SEVERE, "Failed to create the connection pool", e);
        throw new RuntimeException(e.getMessage(), e.getCause());
    }

    HttpServerOptions options = new HttpServerOptions();
    int maxFrameSize = options.getMaxWebsocketFrameSize();

    // Create the actual server
    HttpServer server = vertx.createHttpServer(options);

    // For each incoming websocket connection: create a client connection object
    server.websocketHandler(socket -> {
        if (!socket.path().equals(path)) {
            socket.reject();
            return;
        }
        // TODO: check sub protocols
        new VertxClientConnection(_executorService, socket, connectionPool, maxFrameSize);
    });
    // start to listen
    server.listen(port, result -> {
        if (result.succeeded()) {
            _logger.log(Level.INFO, "Listening on port " + port + " and path '" + path + "'...");
            setStarted(true);
        } else {
            _logger.log(Level.SEVERE, "Failed to listen", result.cause());
            setStartingError(result.cause());
        }
    });
}