Search in sources :

Example 1 with ClientUpgradeRequest

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

the class SimpleEchoClient method main.

public static void main(String[] args) {
    String destUri = "ws://echo.websocket.org";
    if (args.length > 0) {
        destUri = args[0];
    }
    WebSocketClient client = new WebSocketClient();
    SimpleEchoSocket socket = new SimpleEchoSocket();
    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);
        // wait for closed socket connection.
        socket.awaitClose(5, TimeUnit.SECONDS);
    } catch (Throwable t) {
        t.printStackTrace();
    } finally {
        try {
            client.stop();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI)

Example 2 with ClientUpgradeRequest

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

the class AnnotatedServerEndpointTest method assertResponse.

private void assertResponse(String message, String... expectedTexts) throws Exception {
    WebSocketClient client = new WebSocketClient(bufferPool);
    try {
        client.start();
        JettyEchoSocket clientEcho = new JettyEchoSocket();
        URI uri = server.getServerBaseURI().resolve("echo");
        ClientUpgradeRequest req = new ClientUpgradeRequest();
        req.setSubProtocols("echo");
        Future<Session> foo = client.connect(clientEcho, uri, req);
        // wait for connect
        foo.get(1, TimeUnit.SECONDS);
        clientEcho.sendMessage(message);
        Queue<String> msgs = clientEcho.awaitMessages(1);
        String response = msgs.poll();
        for (String expected : expectedTexts) {
            Assert.assertThat("Expected message", response, containsString(expected));
        }
    } finally {
        client.stop();
    }
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session)

Example 3 with ClientUpgradeRequest

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

the class PerMessageDeflateExtensionTest method testPerMessageDeflateDefault.

/**
     * Default configuration for permessage-deflate
     * @throws Exception on test failure
     */
@Test
public void testPerMessageDeflateDefault() throws Exception {
    Assume.assumeTrue("Server has permessage-deflate registered", server.getWebSocketServletFactory().getExtensionFactory().isAvailable("permessage-deflate"));
    Assert.assertThat("server scheme", server.getServerUri().getScheme(), is(scheme));
    int binBufferSize = (int) (msgSize * 1.5);
    WebSocketPolicy serverPolicy = server.getWebSocketServletFactory().getPolicy();
    // Ensure binBufferSize is sane (not smaller then other buffers)
    binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageSize());
    binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageBufferSize());
    binBufferSize = Math.max(binBufferSize, this.inputBufferSize);
    serverPolicy.setMaxBinaryMessageSize(binBufferSize);
    serverPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
    WebSocketClient client = new WebSocketClient(server.getSslContextFactory());
    WebSocketPolicy clientPolicy = client.getPolicy();
    clientPolicy.setMaxBinaryMessageSize(binBufferSize);
    clientPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
    if (inputBufferSize > 0) {
        clientPolicy.setInputBufferSize(inputBufferSize);
    }
    try {
        client.start();
        // Make sure the read times out if there are problems with the implementation
        client.setMaxIdleTimeout(TimeUnit.SECONDS.toMillis(15));
        CaptureSocket clientSocket = new CaptureSocket();
        ClientUpgradeRequest request = new ClientUpgradeRequest();
        request.addExtensions("permessage-deflate");
        request.setSubProtocols("echo");
        Future<Session> fut = client.connect(clientSocket, server.getServerUri(), request);
        // Wait for connect
        Session session = fut.get(30, TimeUnit.SECONDS);
        assertThat("Response.extensions", getNegotiatedExtensionList(session), containsString("permessage-deflate"));
        // Create message
        byte[] msg = new byte[msgSize];
        Random rand = new Random();
        rand.setSeed(8080);
        rand.nextBytes(msg);
        // Calculate sha1
        String sha1 = Sha1Sum.calculate(msg);
        // Client sends first message
        session.getRemote().sendBytes(ByteBuffer.wrap(msg));
        clientSocket.messages.awaitEventCount(1, 5, TimeUnit.SECONDS);
        String echoMsg = clientSocket.messages.poll();
        Assert.assertThat("Echo'd Message", echoMsg, is("binary[sha1=" + sha1 + "]"));
    } finally {
        client.stop();
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Random(java.util.Random) CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 4 with ClientUpgradeRequest

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

the class TestClient method open.

private void open() throws Exception {
    client.getPolicy().setIdleTimeout(_timeout);
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols(_protocol);
    socket = new TestSocket();
    URI wsUri = new URI("ws://" + _host + ":" + _port + "/");
    client.connect(socket, wsUri, request).get(10, TimeUnit.SECONDS);
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) URI(java.net.URI)

Example 5 with ClientUpgradeRequest

use of org.eclipse.jetty.websocket.client.ClientUpgradeRequest 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)

Aggregations

ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)17 URI (java.net.URI)10 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)10 Session (org.eclipse.jetty.websocket.api.Session)9 IOException (java.io.IOException)5 ExecutorService (java.util.concurrent.ExecutorService)5 Test (org.testng.annotations.Test)4 ExecutionException (java.util.concurrent.ExecutionException)3 Test (org.junit.Test)3 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 ParameterException (com.beust.jcommander.ParameterException)1 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 DefaultThreadFactory (io.netty.util.concurrent.DefaultThreadFactory)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 PrintStream (java.io.PrintStream)1 HttpCookie (java.net.HttpCookie)1 KeyManagementException (java.security.KeyManagementException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1