Search in sources :

Example 21 with RemoteEndpoint

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();
    }
}
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)

Example 22 with RemoteEndpoint

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!");
}
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 23 with RemoteEndpoint

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());
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Example 24 with RemoteEndpoint

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));
    }
}
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 25 with RemoteEndpoint

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;
    }
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException)

Aggregations

RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)27 IOException (java.io.IOException)11 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)8 OnWebSocketMessage (org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)7 URI (java.net.URI)6 Session (org.eclipse.jetty.websocket.api.Session)6 Test (org.junit.Test)6 ByteBuffer (java.nio.ByteBuffer)4 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)3 CaptureSocket (org.eclipse.jetty.websocket.server.helper.CaptureSocket)3 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)2 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)2 KeyStore (java.security.KeyStore)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CloseReason (javax.websocket.CloseReason)1 DeploymentException (javax.websocket.DeploymentException)1 WebSocketAdapter (org.eclipse.jetty.websocket.api.WebSocketAdapter)1 OnWebSocketFrame (org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)1 Ignore (org.junit.Ignore)1