Search in sources :

Example 31 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.

the class BasicAppTest method testWebSocketEcho.

@Test
public void testWebSocketEcho() throws Exception {
    WebSocketClient client = new WebSocketClient();
    try {
        client.start();
        CheckSocket socket = new CheckSocket();
        client.connect(socket, serverWebsocketURI.resolve("/echo"));
        socket.awaitOpen(2, TimeUnit.SECONDS);
        socket.sendText("Hello World");
        socket.close(StatusCode.NORMAL, "Test complete");
        socket.awaitClose(2, TimeUnit.SECONDS);
        assertThat("Messages received", socket.getTextMessages().size(), is(1));
        String response = socket.getTextMessages().poll();
        System.err.println(response);
        assertThat("Message[0]", response, is("Hello World"));
    } finally {
        client.stop();
    }
}
Also used : CheckSocket(org.eclipse.jetty.cdi.websocket.CheckSocket) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Test(org.junit.Test)

Example 32 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project jetty.project by eclipse.

the class CdiAppTest method testWebSocket_Info_FieldPresence.

@Test
public void testWebSocket_Info_FieldPresence() throws Exception {
    WebSocketClient client = new WebSocketClient();
    try {
        client.start();
        CheckSocket socket = new CheckSocket();
        client.connect(socket, serverWebsocketURI.resolve("/cdi-info"));
        socket.awaitOpen(2, TimeUnit.SECONDS);
        socket.sendText("info");
        socket.close(StatusCode.NORMAL, "Test complete");
        socket.awaitClose(2, TimeUnit.SECONDS);
        assertThat("Messages received", socket.getTextMessages().size(), is(1));
        String response = socket.getTextMessages().poll();
        System.err.println(response);
        assertThat("Message[0]", response, allOf(containsString("websocketSession is PRESENT"), containsString("httpSession is PRESENT"), containsString("servletContext is PRESENT")));
    } finally {
        client.stop();
    }
}
Also used : CheckSocket(org.eclipse.jetty.cdi.websocket.CheckSocket) Matchers.containsString(org.hamcrest.Matchers.containsString) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Test(org.junit.Test)

Example 33 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project spark by perwendel.

the class GenericIntegrationTest method testWebSocketConversation.

@Test
public void testWebSocketConversation() throws Exception {
    String uri = "ws://localhost:4567/ws";
    WebSocketClient client = new WebSocketClient();
    WebSocketTestClient ws = new WebSocketTestClient();
    try {
        client.start();
        client.connect(ws, URI.create(uri), new ClientUpgradeRequest());
        ws.awaitClose(30, TimeUnit.SECONDS);
    } finally {
        client.stop();
    }
    List<String> events = WebSocketTestHandler.events;
    Assert.assertEquals(3, events.size(), 3);
    Assert.assertEquals("onConnect", events.get(0));
    Assert.assertEquals("onMessage: Hi Spark!", events.get(1));
    Assert.assertEquals("onClose: 1000 Bye!", events.get(2));
}
Also used : WebSocketTestClient(spark.embeddedserver.jetty.websocket.WebSocketTestClient) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Test(org.junit.Test)

Example 34 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project neo4j by neo4j.

the class WebSocketConnectionTest method shouldNotThrowAnyExceptionWhenDataReceivedBeforeClose.

@Test
public void shouldNotThrowAnyExceptionWhenDataReceivedBeforeClose() throws Throwable {
    // Given
    WebSocketClient client = mock(WebSocketClient.class);
    WebSocketConnection conn = new WebSocketConnection(client);
    when(client.isStopped()).thenReturn(true);
    byte[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    // When
    conn.onWebSocketBinary(data, 0, 10);
    conn.recv(10);
// Then
// no exception
}
Also used : WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Test(org.junit.Test)

Example 35 with WebSocketClient

use of org.eclipse.jetty.websocket.client.WebSocketClient in project pulsar by yahoo.

the class ProxyAuthenticationTest method socketTest.

@Test
public void socketTest() throws InterruptedException {
    URI consumeUri = URI.create(CONSUME_URI);
    URI produceUri = URI.create(PRODUCE_URI);
    WebSocketClient consumeClient = new WebSocketClient();
    SimpleConsumerSocket consumeSocket = new SimpleConsumerSocket();
    WebSocketClient produceClient = new WebSocketClient();
    SimpleProducerSocket produceSocket = new SimpleProducerSocket();
    try {
        consumeClient.start();
        ClientUpgradeRequest consumeRequest = new ClientUpgradeRequest();
        Future<Session> consumerFuture = consumeClient.connect(consumeSocket, consumeUri, consumeRequest);
        log.info("Connecting to : {}", consumeUri);
        ClientUpgradeRequest produceRequest = new ClientUpgradeRequest();
        produceClient.start();
        Future<Session> producerFuture = produceClient.connect(produceSocket, produceUri, produceRequest);
        // let it connect
        Thread.sleep(1000);
        Assert.assertTrue(consumerFuture.get().isOpen());
        Assert.assertTrue(producerFuture.get().isOpen());
        consumeSocket.awaitClose(1, TimeUnit.SECONDS);
        produceSocket.awaitClose(1, TimeUnit.SECONDS);
        Assert.assertTrue(produceSocket.getBuffer().size() > 0);
        Assert.assertEquals(produceSocket.getBuffer(), consumeSocket.getBuffer());
    } catch (Throwable t) {
        log.error(t.getMessage());
    } finally {
        ExecutorService executor = newFixedThreadPool(1);
        try {
            executor.submit(() -> {
                try {
                    consumeClient.stop();
                    produceClient.stop();
                    log.info("proxy clients are stopped successfully");
                } catch (Exception e) {
                    log.error(e.getMessage());
                }
            }).get(2, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("failed to close clients ", e);
        }
        executor.shutdownNow();
    }
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test)

Aggregations

WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)40 Test (org.junit.Test)26 URI (java.net.URI)25 Session (org.eclipse.jetty.websocket.api.Session)23 HashMap (java.util.HashMap)10 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)10 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)9 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)9 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)9 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)9 MapPropertySource (org.springframework.core.env.MapPropertySource)9 StandardEnvironment (org.springframework.core.env.StandardEnvironment)9 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)9 ServiceError (com.kixeye.chassis.transport.dto.ServiceError)8 JsonJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.JsonJacksonMessageSerDe)8 XmlMessageSerDe (com.kixeye.chassis.transport.serde.converter.XmlMessageSerDe)8 YamlJacksonMessageSerDe (com.kixeye.chassis.transport.serde.converter.YamlJacksonMessageSerDe)8 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)8 Envelope (com.kixeye.chassis.transport.dto.Envelope)7 Header (com.kixeye.chassis.transport.dto.Header)6