Example usage for io.vertx.core Vertx factory

List of usage examples for io.vertx.core Vertx factory

Introduction

In this page you can find the example usage for io.vertx.core Vertx factory.

Prototype

VertxFactory factory

To view the source code for io.vertx.core Vertx factory.

Click Source Link

Usage

From source file:doug.iotdemo.server.web.WebServer.java

License:Open Source License

private void run() throws MqttException, IOException {
    // MQTT//from w w  w  . ja v  a 2  s . c o m
    String url = "ssl://A2KECYFFLC558H.iot.us-east-1.amazonaws.com:8883";
    // String url = "ssl://localhost:8883";
    mqtt = new MqttAsyncClient(url, "LambdaDevice", new MemoryPersistence());
    MqttConnectOptions options = new MqttConnectOptions();
    options.setKeepAliveInterval(20);
    options.setCleanSession(true);
    mqtt.connect(options);

    // Vertx
    Vertx vertx = Vertx.factory.vertx();

    Router router = Router.router(vertx);

    // router.route().handler(LoggerHandler.create(LoggerFormat.DEFAULT));

    router.get("/api/sensors").handler(this::handleFetch);
    router.put("/api/sensors").handler(this::handlePut);

    router.route().handler(StaticHandler.create("www").setCachingEnabled(false));

    vertx.createHttpServer().requestHandler(router::accept).listen(8080);
    System.out.println("Listening on port 8080");
}

From source file:org.springframework.integration.vertx.WebSocketServer.java

License:Apache License

@Override
public synchronized void start() {
    if (this.running) {
        return;//from w ww .jav  a2  s  .co  m
    }

    HttpServerOptions httpServerOptions = new HttpServerOptions();
    httpServerOptions.setPort(this.getPort());

    this.server = Vertx.factory.vertx().createHttpServer(httpServerOptions).websocketHandler(ws -> {
        final String correlationId = UUID.randomUUID().toString();
        final TcpListener listener = WebSocketServer.this.getListener();
        WebSocketConnection connection = new WebSocketConnection(correlationId, ws, listener);
        WebSocketServer.this.getSender().addNewConnection(connection);
        if (ws.path().equals("/myapp")) {
            ws.handler(data -> {

                listener.onMessage(MessageBuilder.withPayload(data.toString()).setCorrelationId(correlationId)
                        .setHeader(IpHeaders.CONNECTION_ID, correlationId).build());
            });

        } else {
            ws.reject();
        }
    }).requestHandler(req -> {
        if (req.path().equals("/")) {
            req.response().sendFile("ws.html");
        }
    }).listen();
    this.running = true;
}