Search in sources :

Example 41 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class LargeAnnotatedTest method testEcho.

@Test
public void testEcho() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.createWebInf();
    wsb.copyEndpoint(LargeEchoConfiguredSocket.class);
    try {
        wsb.start();
        URI uri = wsb.getServerBaseURI();
        WebAppContext webapp = wsb.createWebAppContext();
        wsb.deployWebapp(webapp);
        // wsb.dump();
        WebSocketClient client = new WebSocketClient(bufferPool);
        try {
            client.getPolicy().setMaxTextMessageSize(128 * 1024);
            client.start();
            JettyEchoSocket clientEcho = new JettyEchoSocket();
            Future<Session> foo = client.connect(clientEcho, uri.resolve("echo/large"));
            // wait for connect
            foo.get(1, TimeUnit.SECONDS);
            // The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
            byte[] txt = new byte[100 * 1024];
            Arrays.fill(txt, (byte) 'o');
            String msg = new String(txt, StandardCharsets.UTF_8);
            clientEcho.sendMessage(msg);
            Queue<String> msgs = clientEcho.awaitMessages(1);
            Assert.assertEquals("Expected message", msg, msgs.poll());
        } finally {
            client.stop();
        }
    } finally {
        wsb.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 42 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class OnMessageReturnTest method testEchoReturn.

@Test
public void testEchoReturn() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.copyWebInf("empty-web.xml");
    wsb.copyClass(EchoReturnEndpoint.class);
    try {
        wsb.start();
        URI uri = wsb.getServerBaseURI();
        WebAppContext webapp = wsb.createWebAppContext();
        wsb.deployWebapp(webapp);
        WebSocketClient client = new WebSocketClient(bufferPool);
        try {
            client.start();
            JettyEchoSocket clientEcho = new JettyEchoSocket();
            Future<Session> future = client.connect(clientEcho, uri.resolve("echoreturn"));
            // wait for connect
            future.get(1, TimeUnit.SECONDS);
            clientEcho.sendMessage("Hello World");
            Queue<String> msgs = clientEcho.awaitMessages(1);
            Assert.assertEquals("Expected message", "Hello World", msgs.poll());
        } finally {
            client.stop();
        }
    } finally {
        wsb.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 43 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class IdleTimeoutTest method assertConnectionTimeout.

private void assertConnectionTimeout(URI uri) throws Exception, IOException, InterruptedException, ExecutionException, TimeoutException {
    WebSocketClient client = new WebSocketClient(bufferPool);
    try {
        client.start();
        JettyEchoSocket clientEcho = new JettyEchoSocket();
        if (LOG.isDebugEnabled())
            LOG.debug("Client Attempting to connnect");
        Future<Session> future = client.connect(clientEcho, uri);
        // wait for connect
        future.get(1, TimeUnit.SECONDS);
        if (LOG.isDebugEnabled())
            LOG.debug("Client Connected");
        // wait 1 second
        if (LOG.isDebugEnabled())
            LOG.debug("Waiting 1 second");
        TimeUnit.SECONDS.sleep(1);
        if (LOG.isDebugEnabled())
            LOG.debug("Waited 1 second");
        if (clientEcho.getClosed() == false) {
            // Try to write
            clientEcho.sendMessage("You shouldn't be there");
            try {
                Queue<String> msgs = clientEcho.awaitMessages(1);
                assertThat("Should not have received messages echoed back", msgs, is(empty()));
            } catch (TimeoutException | InterruptedException e) {
            // valid success path
            }
        }
    } finally {
        client.stop();
    }
}
Also used : WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Session(org.eclipse.jetty.websocket.api.Session) TimeoutException(java.util.concurrent.TimeoutException)

Example 44 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class SessionTest method assertResponse.

private void assertResponse(String requestPath, String requestMessage, String expectedResponse) throws Exception {
    WebSocketClient client = new WebSocketClient(bufferPool);
    try {
        client.start();
        JettyEchoSocket clientEcho = new JettyEchoSocket();
        Future<Session> future = client.connect(clientEcho, serverUri.resolve(requestPath));
        // wait for connect
        future.get(1, TimeUnit.SECONDS);
        clientEcho.sendMessage(requestMessage);
        Queue<String> msgs = clientEcho.awaitMessages(1);
        Assert.assertThat("Expected message", msgs.poll(), is(expectedResponse));
    } finally {
        client.stop();
    }
}
Also used : WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Session(org.eclipse.jetty.websocket.api.Session)

Example 45 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class WebSocketOverSSLTest method testServerSessionRequestURI.

/**
     * Test that server session.upgradeRequest.requestURI reports correctly
     * @throws Exception on test failure
     */
@Test
public void testServerSessionRequestURI() throws Exception {
    Assert.assertThat("server scheme", server.getServerUri().getScheme(), is("wss"));
    WebSocketClient client = new WebSocketClient(server.getSslContextFactory(), null, bufferPool);
    try {
        client.setConnectTimeout(CONNECT_TIMEOUT);
        client.start();
        CaptureSocket clientSocket = new CaptureSocket();
        URI requestUri = server.getServerUri().resolve("/deep?a=b");
        System.err.printf("Request URI: %s%n", requestUri.toASCIIString());
        Future<Session> fut = client.connect(clientSocket, requestUri);
        // wait for connect
        Session session = fut.get(FUTURE_TIMEOUT_SEC, TimeUnit.SECONDS);
        // Generate text frame
        RemoteEndpoint remote = session.getRemote();
        remote.sendString("session.upgradeRequest.requestURI");
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
        // Read frame (hopefully text frame)
        clientSocket.messages.awaitEventCount(1, 30, TimeUnit.SECONDS);
        EventQueue<String> captured = clientSocket.messages;
        String expected = String.format("session.upgradeRequest.requestURI=%s", requestUri.toASCIIString());
        Assert.assertThat("session.upgradeRequest.requestURI", captured.poll(), is(expected));
        // Shutdown the socket
        clientSocket.close();
    } finally {
        client.stop();
    }
}
Also used : CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Aggregations

Session (org.eclipse.jetty.websocket.api.Session)74 Test (org.junit.Test)57 URI (java.net.URI)46 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)32 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)21 ExecutionException (java.util.concurrent.ExecutionException)12 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)9 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Envelope (com.kixeye.chassis.transport.dto.Envelope)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)7 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)7 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7