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

The following examples show how to use io.vertx.core.http.ServerWebSocket#closeHandler() . 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: 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 2
Source File: WebSocketTransport.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
WebSocketListener(ServerWebSocket ws, SockJSSession session) {
  this.ws = ws;
  this.session = session;
  ws.textMessageHandler(this::handleMessages);
  ws.closeHandler(v -> {
    closed = true;
    session.shutdown();
  });
  ws.exceptionHandler(t -> {
    closed = true;
    session.shutdown();
    session.handleException(t);
  });
}
 
Example 3
Source File: RawWebSocketTransport.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
RawWSSockJSSocket(Vertx vertx, Session webSession, User webUser, ServerWebSocket ws) {
  super(vertx, webSession, webUser);
  this.ws = ws;
  ws.closeHandler(v -> {
    // Make sure the writeHandler gets unregistered
    synchronized (RawWSSockJSSocket.this) {
      closed = true;
    }
    RawWSSockJSSocket.super.close();
  });
}
 
Example 4
Source File: WebSocketRouter.java    From festival with Apache License 2.0 4 votes vote down vote up
public void accept(ServerWebSocket serverWebSocket) {
    String path = serverWebSocket.path();

    if (!webSocketRouteMap.containsKey(path)) {
        serverWebSocket.reject();
        return;
    }

    WebSocketRoute webSocketRoute = webSocketRouteMap.get(path);

    webSocketRoute.doOpen(serverWebSocket);

    serverWebSocket.frameHandler(frame -> webSocketRoute.doActive(serverWebSocket, frame));

    serverWebSocket.closeHandler((Void) -> webSocketRoute.doClose(serverWebSocket));
}
 
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();
    }
}