Search in sources :

Example 11 with WebSocket

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

the class SockJSHandlerTest method setupRawWebsocketClient.

/**
 * This does not set up a handler on the websocket
 */
private WebSocket setupRawWebsocketClient(String serverPath) throws InterruptedException {
    String requestURI = serverPath + "/websocket";
    AtomicReference<WebSocket> openedWebSocketReference = new AtomicReference<>();
    CountDownLatch openSocketCountDown = new CountDownLatch(1);
    client.websocket(requestURI, ws -> {
        openedWebSocketReference.set(ws);
        openSocketCountDown.countDown();
        ws.endHandler(v -> testComplete());
        ws.exceptionHandler(this::fail);
    });
    openSocketCountDown.await(5, TimeUnit.SECONDS);
    return openedWebSocketReference.get();
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) WebSocket(io.vertx.core.http.WebSocket)

Example 12 with WebSocket

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

the class SockJSHandlerTest method testTextFrameRawWebSocket.

@Test
public void testTextFrameRawWebSocket() throws InterruptedException {
    String serverPath = "/textecho";
    setupSockJsServer(serverPath, this::echoRequest);
    String message = "hello";
    AtomicReference<String> receivedReply = new AtomicReference<>();
    WebSocket ws = setupRawWebsocketClient(serverPath);
    ws.handler(replyBuffer -> receivedReply.set(replyBuffer.toString()));
    ws.writeFrame(WebSocketFrame.textFrame(message, true));
    await(5, TimeUnit.SECONDS);
    assertEquals("Client reply should have matched request", message, receivedReply.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) WebSocket(io.vertx.core.http.WebSocket) Test(org.junit.Test)

Example 13 with WebSocket

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

the class SubscriptionClient method start.

@Override
public void start() {
    HttpClient httpClient = vertx.createHttpClient(new HttpClientOptions().setDefaultPort(8080));
    httpClient.webSocket("/graphql", websocketRes -> {
        if (websocketRes.succeeded()) {
            WebSocket webSocket = websocketRes.result();
            webSocket.handler(message -> {
                System.out.println(message.toJsonObject().encodePrettily());
            });
            JsonObject request = new JsonObject().put("id", "1").put("type", ApolloWSMessageType.START.getText()).put("payload", new JsonObject().put("query", "subscription { links { url, postedBy { name } } }"));
            webSocket.write(request.toBuffer());
        } else {
            websocketRes.cause().printStackTrace();
        }
    });
}
Also used : HttpClient(io.vertx.core.http.HttpClient) JsonObject(io.vertx.core.json.JsonObject) HttpClientOptions(io.vertx.core.http.HttpClientOptions) WebSocket(io.vertx.core.http.WebSocket)

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