List of usage examples for io.vertx.core.http ServerWebSocket handler
@Override
ServerWebSocket handler(Handler<Buffer> handler);
From source file:fr.thibaultleouay.chat.server.WebSocketServer.java
License:Open Source License
@Override public void start(Future<Void> fut) { vertx.createHttpServer().websocketHandler(new Handler<ServerWebSocket>() { public void handle(final ServerWebSocket ws) { final String id = ws.textHandlerID(); System.out.println("new connection from" + ws.toString() + "id " + id); vertx.eventBus().consumer("chat", message -> { ws.writeFinalTextFrame((String) message.body()); });//from w ww . j a v a 2s . com ws.handler(new Handler<Buffer>() { public void handle(Buffer data) { // When our websocket receive data we publish it to our consumer vertx.eventBus().publish("chat", data.toString()); } }); ws.closeHandler(handler -> { System.out.println("Close WS "); }); } }).requestHandler(new Handler<HttpServerRequest>() { public void handle(HttpServerRequest req) { req.response().end("Chat"); //Not usefull but it display chat on our browser } }).listen(8080); }
From source file:org.atmosphere.vertx.AtmosphereCoordinator.java
License:Apache License
/** * Route the {@link ServerWebSocket} into the {@link AtmosphereFramework} * * @param webSocket the {@link ServerWebSocket} *//*from ww w . j a v a2 s .co m*/ public AtmosphereCoordinator route(ServerWebSocket webSocket) { Map<String, List<String>> paramMap = new QueryStringDecoder("?" + webSocket.query()).parameters(); Map<String, String[]> params = new LinkedHashMap<String, String[]>(paramMap.size()); for (Map.Entry<String, List<String>> entry : paramMap.entrySet()) { params.put(entry.getKey(), entry.getValue().toArray(new String[] {})); } String contentType = "application/json"; if (params.size() == 0) { // TODO: vert.x trim the query string, unfortunately. params.put(X_ATMO_PROTOCOL, new String[] { "true" }); params.put(X_ATMOSPHERE_FRAMEWORK, new String[] { "2.1" }); params.put(X_ATMOSPHERE_TRACKING_ID, new String[] { "0" }); params.put(X_ATMOSPHERE_TRANSPORT, new String[] { "websocket" }); params.put("Content-Type", new String[] { contentType }); } else if (params.containsKey("Content-Type") && params.get("Content-Type").length > 0) { contentType = params.get("Content-Type")[0]; } AtmosphereRequest.Builder requestBuilder = new AtmosphereRequest.Builder(); AtmosphereRequest r = requestBuilder.requestURI(webSocket.path()) .requestURL("http://0.0.0.0" + webSocket.path()).contentType(contentType).pathInfo(webSocket.path()) .queryStrings(params).build(); final WebSocket w = new VertxWebSocket(framework.getAtmosphereConfig(), webSocket); try { webSocketProcessor.open(w, r, AtmosphereResponse.newInstance(framework.getAtmosphereConfig(), r, w)); } catch (IOException e) { logger.debug("", e); } webSocket.handler(new Handler<Buffer>() { @Override public void handle(Buffer data) { webSocketProcessor.invokeWebSocketProtocol(w, data.toString()); } }); webSocket.exceptionHandler(new Handler<Throwable>() { @Override public void handle(Throwable event) { w.close(); logger.debug("", event); webSocketProcessor.close(w, 1006); } }); webSocket.closeHandler(new VoidHandler() { @Override protected void handle() { w.close(); webSocketProcessor.close(w, 1005); } }); return this; }
From source file:org.wisdom.framework.vertx.WebSocketHandler.java
License:Apache License
/** * Handles a web socket connection./*w w w. j a va 2s . c om*/ * * @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)); }