use of org.eclipse.jetty.websocket.api.RemoteEndpoint 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();
}
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint 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.RemoteEndpoint in project jetty.project by eclipse.
the class InfoSocket method onMessage.
@OnWebSocketMessage
public void onMessage(String msg) {
RemoteEndpoint remote = this.session.getRemote();
remote.sendStringByFuture("session.maxTextMessageSize=" + session.getPolicy().getMaxTextMessageSize());
}
use of org.eclipse.jetty.websocket.api.RemoteEndpoint 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.RemoteEndpoint in project CQ-Actions by Cognifide.
the class MessageSocket method sendMessage.
public synchronized boolean sendMessage(String msg) {
confirmed.set(false);
if (session == null) {
return false;
}
try {
RemoteEndpoint remote = session.getRemote();
remote.sendString(msg);
remote.flush();
wait(5000);
return confirmed.getAndSet(false);
} catch (InterruptedException | IOException e) {
LOG.error("Can't send message and receive confirmation", e);
return false;
}
}
Aggregations