Search in sources :

Example 61 with Session

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

the class WebSocketClientTest method testBasicEcho_FromClient.

@Test
public void testBasicEcho_FromClient() throws Exception {
    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(30, TimeUnit.SECONDS);
    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.getOpenSessions();
    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, 30, TimeUnit.SECONDS);
    // wait for response from server
    cliSock.waitForMessage(30, TimeUnit.SECONDS);
    cliSock.assertMessage("Hello World!");
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 62 with Session

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

the class WebSocketClientTest method testLocalRemoteAddress.

@Test
public void testLocalRemoteAddress() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    future.get(30, TimeUnit.SECONDS);
    Assert.assertTrue(wsocket.openLatch.await(1, TimeUnit.SECONDS));
    InetSocketAddress local = wsocket.getSession().getLocalAddress();
    InetSocketAddress remote = wsocket.getSession().getRemoteAddress();
    Assert.assertThat("Local Socket Address", local, notNullValue());
    Assert.assertThat("Remote Socket Address", remote, notNullValue());
    // Hard to validate (in a portable unit test) the local address that was used/bound in the low level Jetty Endpoint
    Assert.assertThat("Local Socket Address / Host", local.getAddress().getHostAddress(), notNullValue());
    Assert.assertThat("Local Socket Address / Port", local.getPort(), greaterThan(0));
    Assert.assertThat("Remote Socket Address / Host", remote.getAddress().getHostAddress(), is(wsUri.getHost()));
    Assert.assertThat("Remote Socket Address / Port", remote.getPort(), greaterThan(0));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 63 with Session

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

the class WebSocketClientTest method testBasicEcho_UsingCallback.

@Test
public void testBasicEcho_UsingCallback() throws Exception {
    client.setMaxIdleTimeout(160000);
    JettyTrackingSocket cliSock = new JettyTrackingSocket();
    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(30, TimeUnit.SECONDS);
    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));
    FutureWriteCallback callback = new FutureWriteCallback();
    cliSock.getSession().getRemote().sendString("Hello World!", callback);
    callback.get(1, TimeUnit.SECONDS);
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) FutureWriteCallback(org.eclipse.jetty.websocket.common.io.FutureWriteCallback) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 64 with Session

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

the class BatchModeTest method testBatchModeAuto.

@Test
public void testBatchModeAuto() throws Exception {
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
    final CountDownLatch latch = new CountDownLatch(1);
    WebSocketAdapter adapter = new WebSocketAdapter() {

        @Override
        public void onWebSocketText(String message) {
            latch.countDown();
        }
    };
    try (Session session = client.connect(adapter, uri).get()) {
        RemoteEndpoint remote = session.getRemote();
        Future<Void> future = remote.sendStringByFuture("batch_mode_on");
        // The write is aggregated and therefore completes immediately.
        future.get(1, TimeUnit.MICROSECONDS);
        // Wait for the echo.
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : WebSocketAdapter(org.eclipse.jetty.websocket.api.WebSocketAdapter) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 65 with Session

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

the class JettyWebSocketSessionProducer method getSession.

@Produces
public Session getSession(InjectionPoint injectionPoint) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("getSession({})", injectionPoint);
    }
    WebSocketScopeContext ctx = WebSocketScopeContext.current();
    if (ctx == null) {
        throw new IllegalStateException("Not in a " + WebSocketScope.class.getName());
    }
    org.eclipse.jetty.websocket.api.Session sess = ctx.getSession();
    if (sess == null) {
        throw new IllegalStateException("No Session Available");
    }
    return sess;
}
Also used : Session(org.eclipse.jetty.websocket.api.Session) Produces(javax.enterprise.inject.Produces)

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