private void testWriteFromConnectHandler(WebsocketVersion version) throws Exception { String path = "/some/path"; Buffer buff = Buffer.buffer("AAA"); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { assertEquals(path, ws.path()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> { Buffer received = Buffer.buffer(); ws.handler(data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
private void testReject(WebsocketVersion version, Integer rejectionStatus, int expectedRejectionStatus) throws Exception { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { assertEquals(path, ws.path()); if (rejectionStatus != null) { ws.reject(rejectionStatus); } else { ws.reject(); } }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version). exceptionHandler(t -> { assertTrue(t instanceof WebsocketRejectedException); assertEquals(expectedRejectionStatus, ((WebsocketRejectedException)t).getStatus()); testComplete(); }). handler(ws -> fail("Should not be called")); }); await(); }
assertEquals(path, ws.path()); assertEquals(query, ws.query()); assertEquals("upgrade", ws.headers().get("Connection"));
assertEquals(path, ws.path()); assertEquals(query, ws.query()); assertEquals("upgrade", ws.headers().get("Connection"));
private void testValidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String clientSubProtocols = "clientproto,commonproto"; String serverSubProtocols = "serverproto,commonproto"; Buffer buff = Buffer.buffer("AAA"); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols(serverSubProtocols)).websocketHandler(ws -> { assertEquals(path, ws.path()); assertNull(ws.subProtocol()); ws.accept(); assertEquals("commonproto", ws.subProtocol()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, clientSubProtocols, ws -> { assertEquals("commonproto", ws.subProtocol()); final Buffer received = Buffer.buffer(); ws.handler(data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
/** * @return the WebSocket handshake path. */ public String path() { String ret = delegate.path(); return ret; }
/** * @return the WebSocket handshake path. */ public String path() { String ret = delegate.path(); return ret; }
/** * Gets the socket path. For SockJS, it returns the full URI. * * @return the socket path */ public String path() { return delegate.path(); }
private void testWriteFromConnectHandler(WebsocketVersion version) throws Exception { String path = "/some/path"; Buffer buff = Buffer.buffer("AAA"); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { assertEquals(path, ws.path()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, ws -> { Buffer received = Buffer.buffer(); ws.handler(data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
private void testReject(WebsocketVersion version, Integer rejectionStatus, int expectedRejectionStatus) throws Exception { String path = "/some/path"; server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT)).websocketHandler(ws -> { assertEquals(path, ws.path()); if (rejectionStatus != null) { ws.reject(rejectionStatus); } else { ws.reject(); } }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocketStream(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version). exceptionHandler(t -> { assertTrue(t instanceof WebsocketRejectedException); assertEquals(expectedRejectionStatus, ((WebsocketRejectedException)t).getStatus()); testComplete(); }). handler(ws -> fail("Should not be called")); }); await(); }
assertEquals(path, ws.path()); assertEquals(query, ws.query()); assertEquals("upgrade", ws.headers().get("Connection"));
assertEquals(path, ws.path()); assertEquals(query, ws.query()); assertEquals("upgrade", ws.headers().get("Connection"));
private void testValidSubProtocol(WebsocketVersion version) throws Exception { String path = "/some/path"; String clientSubProtocols = "clientproto,commonproto"; String serverSubProtocols = "serverproto,commonproto"; Buffer buff = Buffer.buffer("AAA"); server = vertx.createHttpServer(new HttpServerOptions().setPort(DEFAULT_HTTP_PORT).setWebsocketSubProtocols(serverSubProtocols)).websocketHandler(ws -> { assertEquals(path, ws.path()); assertNull(ws.subProtocol()); ws.accept(); assertEquals("commonproto", ws.subProtocol()); ws.writeFrame(WebSocketFrame.binaryFrame(buff, true)); }); server.listen(ar -> { assertTrue(ar.succeeded()); client.websocket(DEFAULT_HTTP_PORT, HttpTestBase.DEFAULT_HTTP_HOST, path, null, version, clientSubProtocols, ws -> { assertEquals("commonproto", ws.subProtocol()); final Buffer received = Buffer.buffer(); ws.handler(data -> { received.appendBuffer(data); if (received.length() == buff.length()) { assertEquals(buff, received); ws.close(); testComplete(); } }); }); }); await(); }
@Override public void handle(ServerWebSocket ws) { String path = ws.path(); for (PatternBinding binding: bindings) { Matcher m = binding.pattern.matcher(path);
@Override public void start() throws Exception { vertx.createHttpServer() .requestHandler(req -> { req.response().end(); }) .websocketHandler(ws -> { if (ws.path().equals("/leaderboard")) { onLeaderBoard(ws); } else if (ws.path().equals("/scoreboard")) { onScoreBoard(ws); } else { ws.reject(); } }).listen(config().getInteger("boardPort", 9004), ar -> { System.out.println("Boards HTTP server started on port " + ar.result().actualPort()); }); }
@Override public void start() throws Exception { httpServer = vertx.createHttpServer(); httpServer .websocketHandler(websocketHandler -> { if (!websocketHandler.path().equals("/vertx/hot")) { websocketHandler.reject(); return; } MessageConsumer<JsonObject> consumer = vertx.eventBus().consumer(TOPIC); consumer.handler(m -> websocketHandler.writeFinalTextFrame(m.body().toString())); websocketHandler.closeHandler((v) -> consumer.unregister()); } ) .listen(notificationPort); logger.info("notification websocket started on: http://localhost:{}", notificationPort); }
/** * 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)); } }
.requestHandler(router::accept) .websocketHandler(socket -> { switch (socket.path()) { case "/stream":