Search in sources :

Example 6 with WebSocket

use of io.vertx.core.http.WebSocket in project vertx-web by vert-x3.

the class SockJSHandlerTest method testSplitLargeReplyRawWebSocket.

@Test
public void testSplitLargeReplyRawWebSocket() throws InterruptedException {
    String serverPath = "/split";
    String largeReply = TestUtils.randomAlphaString(65536 * 5);
    Buffer largeReplyBuffer = Buffer.buffer(largeReply);
    setupSockJsServer(serverPath, (sock, requestBuffer) -> {
        sock.write(largeReplyBuffer);
        sock.close();
    });
    Buffer totalReplyBuffer = Buffer.buffer(largeReplyBuffer.length());
    AtomicInteger receivedReplies = new AtomicInteger(0);
    WebSocket ws = setupRawWebsocketClient(serverPath);
    ws.handler(replyBuffer -> {
        totalReplyBuffer.appendBuffer(replyBuffer);
        receivedReplies.incrementAndGet();
    });
    ws.writeFrame(WebSocketFrame.binaryFrame(Buffer.buffer("hello"), true));
    await(5, TimeUnit.SECONDS);
    int receivedReplyCount = receivedReplies.get();
    assertEquals("Combined reply on client should equal message from server", largeReplyBuffer, totalReplyBuffer);
    assertTrue("Should have received > 1 reply frame, actually received " + receivedReplyCount, receivedReplyCount > 1);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) WebSocket(io.vertx.core.http.WebSocket) Test(org.junit.Test)

Example 7 with WebSocket

use of io.vertx.core.http.WebSocket in project vertx-web by vert-x3.

the class SockJSHandlerTest method testTextFrameSockJs.

@Test
public void testTextFrameSockJs() throws InterruptedException {
    String serverPath = "/text-sockjs";
    setupSockJsServer(serverPath, this::echoRequest);
    List<Buffer> receivedMessages = new ArrayList<>();
    WebSocket openedWebSocket = setupSockJsClient(serverPath, receivedMessages);
    String messageToSend = "[\"testMessage\"]";
    openedWebSocket.writeFrame(WebSocketFrame.textFrame(messageToSend, true));
    await(5, TimeUnit.SECONDS);
    assertEquals("Client should have received 2 messages: the reply and the close.", 2, receivedMessages.size());
    Buffer expectedReply = Buffer.buffer("a" + messageToSend);
    assertEquals("Client reply should have matched request", expectedReply, receivedMessages.get(0));
    assertEquals("Final message should have been a close", SOCKJS_CLOSE_REPLY, receivedMessages.get(1));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArrayList(java.util.ArrayList) WebSocket(io.vertx.core.http.WebSocket) Test(org.junit.Test)

Example 8 with WebSocket

use of io.vertx.core.http.WebSocket in project nem2-sdk-java by nemtech.

the class ListenerVertxTest method simulateWebSocketStartup.

private void simulateWebSocketStartup() throws InterruptedException, ExecutionException, TimeoutException {
    ArgumentCaptor<Handler> webSocketHandlerCapture = ArgumentCaptor.forClass(Handler.class);
    ArgumentCaptor<Handler> bufferHandlerCapture = ArgumentCaptor.forClass(Handler.class);
    when(httpClientMock.websocket(any(RequestOptions.class), webSocketHandlerCapture.capture())).thenReturn(httpClientMock);
    when(webSocketMock.handler(bufferHandlerCapture.capture())).thenReturn(webSocketMock);
    CompletableFuture<Void> future = listener.open();
    Handler<WebSocket> webSocketHandler = webSocketHandlerCapture.getValue();
    Assertions.assertNotNull(webSocketHandler);
    webSocketHandler.handle(webSocketMock);
    Handler<Buffer> bufferHandler = bufferHandlerCapture.getValue();
    Assertions.assertNotNull(bufferHandler);
    Buffer event = new BufferFactoryImpl().buffer(jsonHelper.print(Collections.singletonMap("uid", wsId)));
    bufferHandler.handle(event);
    future.get(3, TimeUnit.SECONDS);
}
Also used : Buffer(io.vertx.core.buffer.Buffer) RequestOptions(io.vertx.core.http.RequestOptions) Handler(io.vertx.core.Handler) BufferFactoryImpl(io.vertx.core.buffer.impl.BufferFactoryImpl) WebSocket(io.vertx.core.http.WebSocket)

Example 9 with WebSocket

use of io.vertx.core.http.WebSocket in project chili-core by codingchili.

the class WebsocketListenerIT method sendRequest.

@Override
public void sendRequest(ResponseListener listener, JsonObject data) {
    context.vertx().createHttpClient().webSocket(port, HOST, CoreStrings.DIR_SEPARATOR, handler -> {
        if (handler.succeeded()) {
            WebSocket webSocket = handler.result();
            webSocket.handler(body -> handleBody(listener, body));
            webSocket.write(Buffer.buffer(data.encode()));
        } else {
            throw new RuntimeException(handler.cause());
        }
    });
}
Also used : WebSocket(io.vertx.core.http.WebSocket)

Example 10 with WebSocket

use of io.vertx.core.http.WebSocket in project vertx-web by vert-x3.

the class SockJSHandlerTest method testCookiesRemoved.

@Test
public void testCookiesRemoved() throws Exception {
    router.route("/cookiesremoved/*").handler(SockJSHandler.create(vertx).socketHandler(sock -> {
        MultiMap headers = sock.headers();
        String cookieHeader = headers.get("cookie");
        assertNotNull(cookieHeader);
        assertEquals("JSESSIONID=wibble", cookieHeader);
        testComplete();
    }));
    MultiMap headers = new CaseInsensitiveHeaders();
    headers.add("cookie", "JSESSIONID=wibble");
    headers.add("cookie", "flibble=floob");
    client.websocket("/cookiesremoved/websocket", headers, ws -> {
        String frame = "foo";
        ws.writeFrame(io.vertx.core.http.WebSocketFrame.textFrame(frame, true));
    });
    await();
}
Also used : MultiMap(io.vertx.core.MultiMap) Test(org.junit.Test) WebSocket(io.vertx.core.http.WebSocket) BodyHandler(io.vertx.ext.web.handler.BodyHandler) AtomicReference(java.util.concurrent.atomic.AtomicReference) LoggerFactory(io.vertx.core.logging.LoggerFactory) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) FrameType(io.vertx.core.http.impl.FrameType) Buffer(io.vertx.core.buffer.Buffer) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BiConsumer(java.util.function.BiConsumer) JsonObject(io.vertx.core.json.JsonObject) WebSocketFrameImpl(io.vertx.core.http.impl.ws.WebSocketFrameImpl) Logger(io.vertx.core.logging.Logger) PermittedOptions(io.vertx.ext.bridge.PermittedOptions) WebSocketFrame(io.vertx.core.http.WebSocketFrame) WebTestBase(io.vertx.ext.web.WebTestBase) CaseInsensitiveHeaders(io.vertx.core.http.CaseInsensitiveHeaders) MultiMap(io.vertx.core.MultiMap) CaseInsensitiveHeaders(io.vertx.core.http.CaseInsensitiveHeaders) Test(org.junit.Test)

Aggregations

WebSocket (io.vertx.core.http.WebSocket)13 Buffer (io.vertx.core.buffer.Buffer)8 Test (org.junit.Test)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ArrayList (java.util.ArrayList)4 WebSocketFrame (io.vertx.core.http.WebSocketFrame)3 JsonObject (io.vertx.core.json.JsonObject)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Handler (io.vertx.core.Handler)2 MultiMap (io.vertx.core.MultiMap)2 HttpClient (io.vertx.core.http.HttpClient)2 WebSocketFrameImpl (io.vertx.core.http.impl.ws.WebSocketFrameImpl)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 BufferFactoryImpl (io.vertx.core.buffer.impl.BufferFactoryImpl)1 CaseInsensitiveHeaders (io.vertx.core.http.CaseInsensitiveHeaders)1 HttpClientOptions (io.vertx.core.http.HttpClientOptions)1 HttpClientRequest (io.vertx.core.http.HttpClientRequest)1 HttpClientResponse (io.vertx.core.http.HttpClientResponse)1 RequestOptions (io.vertx.core.http.RequestOptions)1 FrameType (io.vertx.core.http.impl.FrameType)1