Search in sources :

Example 16 with RemoteEndpoint

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

the class RFCSocket method onText.

@OnWebSocketMessage
public void onText(String message) throws IOException {
    LOG.debug("onText({})", message);
    // trigger a WebSocket server terminated close.
    if (message.equals("CRASH")) {
        throw new RuntimeException("Something bad happened");
    }
    // echo the message back.
    RemoteEndpoint remote = session.getRemote();
    remote.sendString(message, null);
    if (remote.getBatchMode() == BatchMode.ON)
        remote.flush();
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) OnWebSocketMessage(org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage)

Example 17 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 18 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 19 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 20 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)

Aggregations

RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)20 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 IOException (java.io.IOException)4 ByteBuffer (java.nio.ByteBuffer)3 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)3 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 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 WebSocketAdapter (org.eclipse.jetty.websocket.api.WebSocketAdapter)1 OnWebSocketFrame (org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)1 Ignore (org.junit.Ignore)1