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!");
}
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));
}
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);
}
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));
}
}
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;
}
Aggregations