Search in sources :

Example 11 with WebSocketClient

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

the class CdiAppTest method testWebSocketActivated.

@Test
public void testWebSocketActivated() 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");
        socket.close(StatusCode.NORMAL, "Test complete");
        socket.awaitClose(2, TimeUnit.SECONDS);
        assertThat("Messages received", socket.getTextMessages().size(), is(1));
        assertThat("Message[0]", socket.getTextMessages().poll(), is("Hello"));
    } finally {
        client.stop();
    }
}
Also used : CheckSocket(org.eclipse.jetty.cdi.websocket.CheckSocket) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Test(org.junit.Test)

Example 12 with WebSocketClient

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

the class TestClient method main.

public static void main(String[] args) throws Exception {
    String host = "localhost";
    int port = 8080;
    String protocol = null;
    int count = 10;
    int size = 64;
    int fragment = 4000;
    boolean binary = false;
    int clients = 1;
    int delay = 1000;
    for (int i = 0; i < args.length; i++) {
        String a = args[i];
        if ("-p".equals(a) || "--port".equals(a)) {
            port = Integer.parseInt(args[++i]);
        } else if ("-h".equals(a) || "--host".equals(a)) {
            host = args[++i];
        } else if ("-c".equals(a) || "--count".equals(a)) {
            count = Integer.parseInt(args[++i]);
        } else if ("-s".equals(a) || "--size".equals(a)) {
            size = Integer.parseInt(args[++i]);
        } else if ("-f".equals(a) || "--fragment".equals(a)) {
            fragment = Integer.parseInt(args[++i]);
        } else if ("-P".equals(a) || "--protocol".equals(a)) {
            protocol = args[++i];
        } else if ("-v".equals(a) || "--verbose".equals(a)) {
            _verbose = true;
        } else if ("-b".equals(a) || "--binary".equals(a)) {
            binary = true;
        } else if ("-C".equals(a) || "--clients".equals(a)) {
            clients = Integer.parseInt(args[++i]);
        } else if ("-d".equals(a) || "--delay".equals(a)) {
            delay = Integer.parseInt(args[++i]);
        } else if (a.startsWith("-")) {
            usage(args);
        }
    }
    TestClient[] client = new TestClient[clients];
    WebSocketClient wsclient = new WebSocketClient(bufferPool);
    try {
        wsclient.start();
        __start = System.currentTimeMillis();
        protocol = protocol == null ? "echo" : protocol;
        for (int i = 0; i < clients; i++) {
            client[i] = new TestClient(wsclient, host, port, protocol, 60000);
            client[i].open();
        }
        System.out.println("Jetty WebSocket PING " + host + ":" + port + " (" + new InetSocketAddress(host, port) + ") " + clients + " clients " + protocol);
        for (int p = 0; p < count; p++) {
            long next = System.currentTimeMillis() + delay;
            byte op = OpCode.TEXT;
            if (binary) {
                op = OpCode.BINARY;
            }
            byte[] data = null;
            switch(op) {
                case OpCode.TEXT:
                    {
                        StringBuilder b = new StringBuilder();
                        while (b.length() < size) {
                            b.append('A' + __random.nextInt(26));
                        }
                        data = b.toString().getBytes(StandardCharsets.UTF_8);
                        break;
                    }
                case OpCode.BINARY:
                    {
                        data = new byte[size];
                        __random.nextBytes(data);
                        break;
                    }
            }
            for (int i = 0; i < clients; i++) {
                client[i].send(op, data, fragment);
            }
            while (System.currentTimeMillis() < next) {
                Thread.sleep(10);
            }
        }
    } finally {
        for (int i = 0; i < clients; i++) {
            if (client[i] != null) {
                client[i].disconnect();
            }
        }
        long duration = System.currentTimeMillis() - __start;
        System.out.println("--- " + host + " websocket ping statistics using " + clients + " connection" + (clients > 1 ? "s" : "") + " ---");
        System.out.printf("%d/%d frames sent/recv, %d/%d mesg sent/recv, time %dms %dm/s %.2fbps%n", __framesSent, __framesReceived.get(), __messagesSent, __messagesReceived.get(), duration, ((1000L * __messagesReceived.get()) / duration), (1000.0D * __messagesReceived.get() * 8 * size) / duration / 1024 / 1024);
        System.out.printf("rtt min/ave/max = %.3f/%.3f/%.3f ms\n", __minDuration.get() / 1000000.0, __messagesReceived.get() == 0 ? 0.0 : (__totalTime.get() / __messagesReceived.get() / 1000000.0), __maxDuration.get() / 1000000.0);
        wsclient.stop();
    }
    bufferPool.assertNoLeaks();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient)

Example 13 with WebSocketClient

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

the class EchoWebSocketsTest method testWebsocketClient.

@Test
public void testWebsocketClient() throws Exception {
    String destUri = "ws://localhost:9191";
    WebSocketClient client = new WebSocketClient();
    SimpleEchoSocket socket = new SimpleEchoSocket();
    socket.getToSendMessages().add("Hello");
    socket.getToSendMessages().add("Thanks for the conversation.");
    socket.getToSendMessages().add(createString(31000));
    socket.getToSendMessages().add(createString(65400));
    try {
        client.start();
        URI echoUri = new URI(destUri);
        ClientUpgradeRequest request = new ClientUpgradeRequest();
        client.connect(socket, echoUri, request);
        System.out.printf("Connecting to : %s%n", echoUri);
        socket.awaitClose(5, TimeUnit.SECONDS);
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        try {
            client.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Assert.assertEquals(4, socket.getReceivedMessages().size());
    Assert.assertEquals("Hello", socket.getReceivedMessages().get(0));
    Assert.assertEquals("Thanks for the conversation.", socket.getReceivedMessages().get(1));
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) IOException(java.io.IOException) Test(org.junit.Test)

Example 14 with WebSocketClient

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

the class WebSocketConnectionTest method shouldThrowIOExceptionWhenNotEnoughDataReceivedBeforeClose.

@Test
public void shouldThrowIOExceptionWhenNotEnoughDataReceivedBeforeClose() throws Throwable {
    // Given
    WebSocketClient client = mock(WebSocketClient.class);
    WebSocketConnection conn = new WebSocketConnection(client);
    when(client.isStopped()).thenReturn(true, true);
    byte[] data = { 0, 1, 2, 3 };
    // When && Then
    conn.onWebSocketBinary(data, 0, 4);
    expectedException.expect(IOException.class);
    expectedException.expectMessage("Connection closed while waiting for data from the server.");
    conn.recv(10);
}
Also used : WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Test(org.junit.Test)

Example 15 with WebSocketClient

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

the class GoAgentServerWebSocketClientBuilder method build.

@Override
public WebSocketClient build() throws Exception {
    SslContextFactory sslContextFactory = sslVerificationMode == SslVerificationMode.NONE ? new TrustAllSSLContextFactory() : new SslContextFactory();
    sslContextFactory.setNeedClientAuth(true);
    sslContextFactory.setKeyStore(agentKeystore());
    sslContextFactory.setKeyStorePassword(keystorePassword());
    sslContextFactory.setKeyManagerPassword(keystorePassword());
    if (rootCertFile != null) {
        sslContextFactory.setTrustStore(agentTruststore());
        sslContextFactory.setTrustStorePassword(keystorePassword());
    }
    if (sslVerificationMode == SslVerificationMode.NO_VERIFY_HOST) {
        sslContextFactory.setEndpointIdentificationAlgorithm(null);
    }
    WebSocketClient client = new WebSocketClient(sslContextFactory);
    client.setMaxIdleTimeout(systemEnvironment.getWebsocketMaxIdleTime());
    return client;
}
Also used : SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient)

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