Search in sources :

Example 11 with Session

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

the class BasicEndpointTest method testEcho.

@Test
public void testEcho() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.copyWebInf("basic-echo-endpoint-config-web.xml");
    // the endpoint (extends javax.websocket.Endpoint)
    wsb.copyClass(BasicEchoEndpoint.class);
    // the configuration (adds the endpoint)
    wsb.copyClass(BasicEchoEndpointConfigContextListener.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("echo"));
            // 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 12 with Session

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

the class SessionTest method testBasicEcho_FromClient.

@Test
// TODO fix frequent failure
@Ignore
public void testBasicEcho_FromClient() throws Exception {
    WebSocketClient client = new WebSocketClient();
    client.start();
    try {
        JettyTrackingSocket cliSock = new JettyTrackingSocket();
        client.getPolicy().setIdleTimeout(10000);
        URI wsUri = server.getWsUri();
        ClientUpgradeRequest request = new ClientUpgradeRequest();
        request.setSubProtocols("echo");
        Future<Session> future = client.connect(cliSock, wsUri, request);
        final IBlockheadServerConnection srvSock = server.accept();
        srvSock.upgrade();
        Session sess = future.get(30000, TimeUnit.MILLISECONDS);
        Assert.assertThat("Session", sess, notNullValue());
        Assert.assertThat("Session.open", sess.isOpen(), is(true));
        Assert.assertThat("Session.upgradeRequest", sess.getUpgradeRequest(), notNullValue());
        Assert.assertThat("Session.upgradeResponse", sess.getUpgradeResponse(), notNullValue());
        cliSock.assertWasOpened();
        cliSock.assertNotClosed();
        Collection<WebSocketSession> sessions = client.getBeans(WebSocketSession.class);
        Assert.assertThat("client.connectionManager.sessions.size", sessions.size(), is(1));
        RemoteEndpoint remote = cliSock.getSession().getRemote();
        remote.sendStringByFuture("Hello World!");
        if (remote.getBatchMode() == BatchMode.ON) {
            remote.flush();
        }
        srvSock.echoMessage(1, 30000, TimeUnit.MILLISECONDS);
        // wait for response from server
        cliSock.waitForMessage(30000, TimeUnit.MILLISECONDS);
        Set<WebSocketSession> open = client.getOpenSessions();
        Assert.assertThat("(Before Close) Open Sessions.size", open.size(), is(1));
        cliSock.assertMessage("Hello World!");
        cliSock.close();
        srvSock.close();
        cliSock.waitForClose(30000, TimeUnit.MILLISECONDS);
        open = client.getOpenSessions();
        // TODO this sometimes fails!
        Assert.assertThat("(After Close) Open Sessions.size", open.size(), is(0));
    } finally {
        client.stop();
    }
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) URI(java.net.URI) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with Session

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

the class SlowClientTest method testClientSlowToSend.

@Test
@Slow
public void testClientSlowToSend() throws Exception {
    JettyTrackingSocket tsocket = new JettyTrackingSocket();
    client.getPolicy().setIdleTimeout(60000);
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(tsocket, wsUri);
    IBlockheadServerConnection sconnection = server.accept();
    sconnection.setSoTimeout(60000);
    sconnection.upgrade();
    // Confirm connected
    future.get(30, TimeUnit.SECONDS);
    tsocket.waitForConnected(30, TimeUnit.SECONDS);
    int messageCount = 10;
    // Setup server read thread
    ServerReadThread reader = new ServerReadThread(sconnection, messageCount);
    reader.start();
    // Have client write slowly.
    ClientWriteThread writer = new ClientWriteThread(tsocket.getSession());
    writer.setMessageCount(messageCount);
    writer.setMessage("Hello");
    writer.setSlowness(10);
    writer.start();
    writer.join();
    reader.waitForExpectedMessageCount(1, TimeUnit.MINUTES);
    // Verify receive
    Assert.assertThat("Frame Receive Count", reader.getFrameCount(), is(messageCount));
    // Close
    tsocket.getSession().close(StatusCode.NORMAL, "Done");
    Assert.assertTrue("Client Socket Closed", tsocket.closeLatch.await(3, TimeUnit.MINUTES));
    tsocket.assertCloseCode(StatusCode.NORMAL);
    // stop reading
    reader.cancel();
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 14 with Session

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

the class ClientConnectTest method testUpgradeRequest.

@Test
public void testUpgradeRequest() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection connection = server.accept();
    connection.upgrade();
    Session sess = future.get(30, TimeUnit.SECONDS);
    wsocket.waitForConnected(1, TimeUnit.SECONDS);
    assertThat("Connect.UpgradeRequest", wsocket.connectUpgradeRequest, notNullValue());
    assertThat("Connect.UpgradeResponse", wsocket.connectUpgradeResponse, notNullValue());
    sess.close();
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 15 with Session

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

the class ClientConnectTest method testBadHandshake.

@Test
public void testBadHandshake() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection connection = server.accept();
    connection.readRequest();
    // no upgrade, just fail with a 404 error
    connection.respond("HTTP/1.1 404 NOT FOUND\r\n" + "Content-Length: 0\r\n" + "\r\n");
    // The attempt to get upgrade response future should throw error
    try {
        future.get(30, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> UpgradeException");
    } catch (ExecutionException e) {
        // Expected Path
        UpgradeException ue = assertExpectedError(e, wsocket, UpgradeException.class);
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI(), notNullValue());
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI().toASCIIString(), is(wsUri.toASCIIString()));
        Assert.assertThat("UpgradeException.responseStatusCode", ue.getResponseStatusCode(), is(404));
    }
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ExecutionException(java.util.concurrent.ExecutionException) 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