Java Code Examples for io.vertx.core.http.ServerWebSocket#handler()

The following examples show how to use io.vertx.core.http.ServerWebSocket#handler() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: WSLocalHandler.java    From vert.x-microservice with Apache License 2.0 6 votes vote down vote up
private void sendToWSService(final ServerWebSocket serverSocket, final String path, final WSEndpoint endpoint) {
    final EventBus eventBus = vertx.eventBus();
    serverSocket.handler(handler -> {
                try {
                    log("send WS:+ " + endpoint.getUrl());
                    eventBus.send(path, Serializer.serialize(new WSDataWrapper(endpoint, handler.getBytes())), new DeliveryOptions().setSendTimeout(GlobalKeyHolder.DEFAULT_SERVICE_TIMEOUT));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


    );
    serverSocket.resume();
    //TODO set close handler!!
}
 
Example 2
Source File: WebSocketHandler.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Handles a web socket connection.
 *
 * @param socket the opening socket.
 */
@Override
public void handle(final ServerWebSocket socket) {
    LOGGER.info("New web socket connection {}, {}", socket, socket.uri());

    if (! configuration.accept(socket.uri())) {
        LOGGER.warn("Web Socket connection denied on {} by {}", socket.uri(), configuration.name());
        return;
    }

    final Socket sock = new Socket(socket);
    accessor.getDispatcher().addSocket(socket.path(), sock);

    socket.closeHandler(event -> {
        LOGGER.info("Web Socket closed {}, {}", socket, socket.uri());
        accessor.getDispatcher().removeSocket(socket.path(), sock);
    });

    socket.handler(event -> accessor.getDispatcher().received(socket.path(), event.getBytes(), sock));

}
 
Example 3
Source File: WSClusterHandler.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
private void sendToWSService(final ServerWebSocket serverSocket, final EventBus eventBus, final String path, final WSEndpoint endpoint) {
    serverSocket.handler(handler -> {
                try {
                    eventBus.send(path, Serializer.serialize(new WSDataWrapper(endpoint, handler.getBytes())), new DeliveryOptions().setSendTimeout(GlobalKeyHolder.DEFAULT_SERVICE_TIMEOUT));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }


    );
    serverSocket.resume();
    //TODO set close handler!!
}
 
Example 4
Source File: WSRouteMatcher.java    From vert.x-microservice with Apache License 2.0 5 votes vote down vote up
public void accept(ServerWebSocket ws){
    final String path = ws.path();
    if(routes.containsKey(path)) {
           sessions.put(ws,path);
           Handler handler = routes.get(path);
           ws.handler(handler);
    }else {
        //TODO error handling
    }
}
 
Example 5
Source File: ConnectHandler.java    From nassh-relay with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void handle(final ServerWebSocket ws) {
    ws.setWriteQueueMaxSize(Constants.QUEUEMAXSIZE);
    final MultiMap params = params(ws.uri());
    if (ws.path().equals("/connect") && params.contains("sid") && params.contains("ack") && params.contains("pos")) {
        final UUID sid = UUID.fromString(params.get("sid"));
        final LocalMap<String, Session> map = vertx.sharedData().getLocalMap(Constants.SESSIONS);
        final Session session = map.get(sid.toString());
        if (session == null || !session.isActive()) {
            ws.reject();
            return;
        }
        session.setRead_count(Integer.parseInt(params.get("ack")));
        session.setWrite_count(Integer.parseInt(params.get("pos")));
        final TransferObserver observer = new TransferObserver(session, ws);
        final TransferQueue queue;
        try {
            queue = QueueFactory.getQueue(sid.toString());
        } catch (final NoSuchQueueException ex) {
            logger.warn(ex, ex.fillInStackTrace());
            ws.reject();
            ws.close();
            return;
        }
        if (queue.countObservers() == 0) {
            queue.addObserver(observer);
        }
        final Buffer buffer = queue.peek();
        if (buffer != null) {
            if (!ws.writeQueueFull()) {
                final Buffer ackbuffer = Buffer.buffer();
                ackbuffer.setInt(0, session.getWrite_count());
                ackbuffer.setBuffer(4, buffer);
                ws.write(ackbuffer);
                queue.remove(buffer);
            } else {
                ws.pause();
            }
        }
        logger.debug("connected");
        ws.drainHandler(v -> ws.resume());
        ws.handler(data -> {
            if (!session.isActive()) {
                ws.close();
                return;
            }
            if (data.length() < 4) {
                logger.warn("wrong frame format");
                return;
            }
            session.setWrite_count(session.getWrite_count() + data.length() - 4);
            vertx.eventBus().publish(session.getHandler(), data.getBuffer(4, data.length()));
        });
        ws.closeHandler(v -> {
            queue.deleteObservers();
            logger.debug("disconnected");
        });
    } else {
        ws.reject();
    }
}