Search in sources :

Example 1 with SockJSHandlerOptions

use of io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions in project vertx-web by vert-x3.

the class WebExamples method example44.

public void example44(Vertx vertx) {
    Router router = Router.router(vertx);
    SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
    sockJSHandler.socketHandler(sockJSSocket -> {
        // Just echo the data back
        sockJSSocket.handler(sockJSSocket::write);
    });
    router.route("/myapp/*").handler(sockJSHandler);
}
Also used : SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 2 with SockJSHandlerOptions

use of io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions in project vertx-openshift-it by cescoffier.

the class SockVerticle method setupSockJSEventBusHandler.

private void setupSockJSEventBusHandler(Transport usedTransport) {
    status.put(usedTransport.toString() + "-EB", false);
    status.put(usedTransport.toString() + "-sock", false);
    SockJSHandlerOptions handlerOptions = new SockJSHandlerOptions();
    Arrays.stream(Transport.values()).filter(t -> usedTransport != t).map(Enum::name).map(handlerOptions::addDisabledTransport);
    router.route("/eventbus/" + usedTransport.name() + "/*").handler(SockJSHandler.create(vertx, handlerOptions).bridge(PERMITTED_OPTIONS, event -> {
        if (event.type() == BridgeEventType.SOCKET_CREATED) {
            System.out.println(usedTransport.name() + " socket was created");
        }
        if (event.type() == BridgeEventType.SEND) {
            vertx.eventBus().publish(TO_CLIENT, new JsonObject().put("content", event.getRawMessage()).encode());
        }
        event.complete(true);
        if (event.type() == BridgeEventType.SOCKET_CLOSED) {
            System.out.println(usedTransport.name() + " socket was closed");
        }
    }));
    router.route("/sock/" + usedTransport.name() + "/*").handler(SockJSHandler.create(vertx, handlerOptions).socketHandler(socket -> {
        socket.handler(content -> {
            status.put(content.toString() + "-sock", true);
            socket.write(content);
        });
    }));
}
Also used : Transport(io.vertx.ext.web.handler.sockjs.Transport) Arrays(java.util.Arrays) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler) StaticHandler(io.vertx.ext.web.handler.StaticHandler) AbstractVerticle(io.vertx.core.AbstractVerticle) BridgeEventType(io.vertx.ext.bridge.BridgeEventType) HttpHeaders(io.vertx.core.http.HttpHeaders) Router(io.vertx.ext.web.Router) JsonObject(io.vertx.core.json.JsonObject) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) JsonObject(io.vertx.core.json.JsonObject)

Example 3 with SockJSHandlerOptions

use of io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions in project vertx-web by vert-x3.

the class SockJSHandlerImpl method installTestApplications.

public static void installTestApplications(Router router, Vertx vertx) {
    // These applications are required by the SockJS protocol and QUnit tests
    router.route("/echo/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(sock -> sock.handler(sock::write)));
    router.route("/close/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(SockJSSocket::close));
    router.route("/disabled_websocket_echo/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096).addDisabledTransport("WEBSOCKET")).socketHandler(sock -> sock.handler(sock::write)));
    router.route("/ticker/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(sock -> {
        long timerID = vertx.setPeriodic(1000, tid -> sock.write(buffer("tick!")));
        sock.endHandler(v -> vertx.cancelTimer(timerID));
    }));
    router.route("/amplify/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(sock -> sock.handler(data -> {
        String str = data.toString();
        int n = Integer.valueOf(str);
        if (n < 0 || n > 19) {
            n = 1;
        }
        int num = (int) Math.pow(2, n);
        Buffer buff = buffer(num);
        for (int i = 0; i < num; i++) {
            buff.appendByte((byte) 'x');
        }
        sock.write(buff);
    })));
    router.route("/broadcast/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096)).socketHandler(new Handler<SockJSSocket>() {

        Set<String> connections = new HashSet<>();

        public void handle(SockJSSocket sock) {
            connections.add(sock.writeHandlerID());
            sock.handler(buffer -> {
                for (String actorID : connections) {
                    vertx.eventBus().publish(actorID, buffer);
                }
            });
            sock.endHandler(v -> connections.remove(sock.writeHandlerID()));
        }
    }));
    router.route("/cookie_needed_echo/*").handler(SockJSHandler.create(vertx, new SockJSHandlerOptions().setMaxBytesStreaming(4096).setInsertJSESSIONID(true)).socketHandler(sock -> sock.handler(sock::write)));
}
Also used : SockJSSocket(io.vertx.ext.web.handler.sockjs.SockJSSocket) Iterator(java.util.Iterator) MessageDigest(java.security.MessageDigest) Date(java.util.Date) HttpServer(io.vertx.core.http.HttpServer) Vertx(io.vertx.core.Vertx) Router(io.vertx.ext.web.Router) SimpleDateFormat(java.text.SimpleDateFormat) Set(java.util.Set) RoutingContext(io.vertx.ext.web.RoutingContext) BridgeEvent(io.vertx.ext.web.handler.sockjs.BridgeEvent) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) Transport(io.vertx.ext.web.handler.sockjs.Transport) HashSet(java.util.HashSet) List(java.util.List) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler) Buffer(io.vertx.core.buffer.Buffer) LocalMap(io.vertx.core.shareddata.LocalMap) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Handler(io.vertx.core.Handler) Logger(io.vertx.core.logging.Logger) SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) BridgeOptions(io.vertx.ext.web.handler.sockjs.BridgeOptions) Buffer(io.vertx.core.buffer.Buffer) SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) Set(java.util.Set) HashSet(java.util.HashSet) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler) Handler(io.vertx.core.Handler) SockJSSocket(io.vertx.ext.web.handler.sockjs.SockJSSocket)

Example 4 with SockJSHandlerOptions

use of io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions in project vertx-web by vert-x3.

the class WebExamples method example43.

public void example43(Vertx vertx) {
    Router router = Router.router(vertx);
    SockJSHandlerOptions options = new SockJSHandlerOptions().setHeartbeatInterval(2000);
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options);
    router.route("/myapp/*").handler(sockJSHandler);
}
Also used : SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) SockJSHandler(io.vertx.ext.web.handler.sockjs.SockJSHandler)

Example 5 with SockJSHandlerOptions

use of io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions in project api-framework by vinscom.

the class SockJSHandlerInstance method start.

@StartService
public void start() {
    // Eventbus handle
    SockJSHandlerOptions sockJSHandlerOptions = (new SockJSHandlerOptions()).addDisabledTransport(Transport.EVENT_SOURCE.toString()).addDisabledTransport(Transport.HTML_FILE.toString()).addDisabledTransport(Transport.JSON_P.toString()).addDisabledTransport(Transport.XHR.toString()).setInsertJSESSIONID(false);
    mSockJSHandler = SockJSHandler.create(getVertx(), sockJSHandlerOptions).bridge(getBridgeOptions(), getBridgeEventHandler());
}
Also used : SockJSHandlerOptions(io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions) StartService(in.erail.glue.annotation.StartService)

Aggregations

SockJSHandlerOptions (io.vertx.ext.web.handler.sockjs.SockJSHandlerOptions)5 SockJSHandler (io.vertx.ext.web.handler.sockjs.SockJSHandler)4 Router (io.vertx.ext.web.Router)2 BridgeOptions (io.vertx.ext.web.handler.sockjs.BridgeOptions)2 Transport (io.vertx.ext.web.handler.sockjs.Transport)2 StartService (in.erail.glue.annotation.StartService)1 AbstractVerticle (io.vertx.core.AbstractVerticle)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 HttpHeaders (io.vertx.core.http.HttpHeaders)1 HttpServer (io.vertx.core.http.HttpServer)1 HttpServerResponse (io.vertx.core.http.HttpServerResponse)1 JsonObject (io.vertx.core.json.JsonObject)1 Logger (io.vertx.core.logging.Logger)1 LoggerFactory (io.vertx.core.logging.LoggerFactory)1 LocalMap (io.vertx.core.shareddata.LocalMap)1 BridgeEventType (io.vertx.ext.bridge.BridgeEventType)1 PermittedOptions (io.vertx.ext.bridge.PermittedOptions)1 RoutingContext (io.vertx.ext.web.RoutingContext)1