Search in sources :

Example 1 with RemoteEndpoint

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

the class SessionTest method testBasicEcho_FromClient.

@Test
// TODO fix frequent failure
@Ignore
public void testBasicEcho_FromClient() throws Exception {
    WebSocketClient client = new WebSocketClient();
    client.start();
    try {
        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(30000, TimeUnit.MILLISECONDS);
        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));
        RemoteEndpoint remote = cliSock.getSession().getRemote();
        remote.sendStringByFuture("Hello World!");
        if (remote.getBatchMode() == BatchMode.ON) {
            remote.flush();
        }
        srvSock.echoMessage(1, 30000, TimeUnit.MILLISECONDS);
        // wait for response from server
        cliSock.waitForMessage(30000, TimeUnit.MILLISECONDS);
        Set<WebSocketSession> open = client.getOpenSessions();
        Assert.assertThat("(Before Close) Open Sessions.size", open.size(), is(1));
        cliSock.assertMessage("Hello World!");
        cliSock.close();
        srvSock.close();
        cliSock.waitForClose(30000, TimeUnit.MILLISECONDS);
        open = client.getOpenSessions();
        // TODO this sometimes fails!
        Assert.assertThat("(After Close) Open Sessions.size", open.size(), is(0));
    } finally {
        client.stop();
    }
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) URI(java.net.URI) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with RemoteEndpoint

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

the class ClientWriteThread method run.

@Override
public void run() {
    final AtomicInteger m = new AtomicInteger();
    try {
        LOG.debug("Writing {} messages to connection {}", messageCount);
        LOG.debug("Artificial Slowness {} ms", slowness);
        Future<Void> lastMessage = null;
        RemoteEndpoint remote = session.getRemote();
        while (m.get() < messageCount) {
            lastMessage = remote.sendStringByFuture(message + "/" + m.get() + "/");
            m.incrementAndGet();
            if (slowness > 0) {
                TimeUnit.MILLISECONDS.sleep(slowness);
            }
        }
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
        // block on write of last message
        if (lastMessage != null)
            // block on write
            lastMessage.get(2, TimeUnit.MINUTES);
    } catch (Exception e) {
        LOG.warn(e);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint)

Example 3 with RemoteEndpoint

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

the class MyEchoSocket method onWebSocketText.

@Override
public void onWebSocketText(String message) {
    if (isNotConnected()) {
        return;
    }
    try {
        // echo the data back
        RemoteEndpoint remote = getRemote();
        remote.sendString(message);
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
    } catch (IOException e) {
        throw new RuntimeIOException(e);
    }
}
Also used : RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) RuntimeIOException(org.eclipse.jetty.io.RuntimeIOException)

Example 4 with RemoteEndpoint

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

the class BigEchoSocket method onText.

@OnWebSocketMessage
public void onText(Session session, String message) throws IOException {
    if (!session.isOpen()) {
        LOG.warn("Session is closed");
        return;
    }
    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 5 with RemoteEndpoint

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

the class EchoFragmentSocket method onFrame.

@OnWebSocketFrame
public void onFrame(Session session, Frame frame) {
    if (!frame.getType().isData()) {
        // Don't process non-data frames
        return;
    }
    ByteBuffer data = frame.getPayload();
    int half = data.remaining() / 2;
    ByteBuffer buf1 = data.slice();
    ByteBuffer buf2 = data.slice();
    buf1.limit(half);
    buf2.position(half);
    RemoteEndpoint remote = session.getRemote();
    try {
        switch(frame.getType()) {
            case BINARY:
                remote.sendBytes(buf1, null);
                remote.sendBytes(buf2, null);
                break;
            case TEXT:
                // NOTE: This impl is not smart enough to split on a UTF8 boundary
                remote.sendString(BufferUtil.toUTF8String(buf1), null);
                remote.sendString(BufferUtil.toUTF8String(buf2), null);
                break;
            default:
                throw new IOException("Unexpected frame type: " + frame.getType());
        }
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}
Also used : RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) OnWebSocketFrame(org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)

Aggregations

RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)25 IOException (java.io.IOException)9 RuntimeIOException (org.eclipse.jetty.io.RuntimeIOException)7 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)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 CloseReason (javax.websocket.CloseReason)1 WebSocketAdapter (org.eclipse.jetty.websocket.api.WebSocketAdapter)1 OnWebSocketFrame (org.eclipse.jetty.websocket.api.annotations.OnWebSocketFrame)1 Ignore (org.junit.Ignore)1