Search in sources :

Example 1 with FutureWriteCallback

use of org.eclipse.jetty.websocket.common.io.FutureWriteCallback in project jetty.project by eclipse.

the class AbstractJsrRemote method sendObjectViaFuture.

@SuppressWarnings({ "rawtypes", "unchecked" })
public Future<Void> sendObjectViaFuture(Object data) {
    assertMessageNotNull(data);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendObject({})", data);
    }
    Encoder encoder = encoders.getEncoderFor(data.getClass());
    if (encoder == null) {
        throw new IllegalArgumentException("No encoder for type: " + data.getClass());
    }
    if (encoder instanceof Encoder.Text) {
        Encoder.Text text = (Encoder.Text) encoder;
        try {
            String msg = text.encode(data);
            return jettyRemote.sendStringByFuture(msg);
        } catch (EncodeException e) {
            return new EncodeFailedFuture(data, text, Encoder.Text.class, e);
        }
    } else if (encoder instanceof Encoder.TextStream) {
        Encoder.TextStream etxt = (Encoder.TextStream) encoder;
        FutureWriteCallback callback = new FutureWriteCallback();
        try (MessageWriter writer = new MessageWriter(session)) {
            writer.setCallback(callback);
            etxt.encode(data, writer);
            return callback;
        } catch (EncodeException | IOException e) {
            return new EncodeFailedFuture(data, etxt, Encoder.Text.class, e);
        }
    } else if (encoder instanceof Encoder.Binary) {
        Encoder.Binary ebin = (Encoder.Binary) encoder;
        try {
            ByteBuffer buf = ebin.encode(data);
            return jettyRemote.sendBytesByFuture(buf);
        } catch (EncodeException e) {
            return new EncodeFailedFuture(data, ebin, Encoder.Binary.class, e);
        }
    } else if (encoder instanceof Encoder.BinaryStream) {
        Encoder.BinaryStream ebin = (Encoder.BinaryStream) encoder;
        FutureWriteCallback callback = new FutureWriteCallback();
        try (MessageOutputStream out = new MessageOutputStream(session)) {
            out.setCallback(callback);
            ebin.encode(data, out);
            return callback;
        } catch (EncodeException | IOException e) {
            return new EncodeFailedFuture(data, ebin, Encoder.Binary.class, e);
        }
    }
    throw new IllegalArgumentException("Unknown encoder type: " + encoder);
}
Also used : FutureWriteCallback(org.eclipse.jetty.websocket.common.io.FutureWriteCallback) EncodeException(javax.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) MessageOutputStream(org.eclipse.jetty.websocket.common.message.MessageOutputStream) Encoder(javax.websocket.Encoder) MessageWriter(org.eclipse.jetty.websocket.common.message.MessageWriter) EncodeFailedFuture(org.eclipse.jetty.websocket.jsr356.encoders.EncodeFailedFuture)

Example 2 with FutureWriteCallback

use of org.eclipse.jetty.websocket.common.io.FutureWriteCallback in project jetty.project by eclipse.

the class WebSocketRemoteEndpoint method sendAsyncFrame.

/**
     * Internal
     *
     * @param frame the frame to write
     * @return the future for the network write of the frame
     */
private Future<Void> sendAsyncFrame(WebSocketFrame frame) {
    FutureWriteCallback future = new FutureWriteCallback();
    uncheckedSendFrame(frame, future);
    return future;
}
Also used : FutureWriteCallback(org.eclipse.jetty.websocket.common.io.FutureWriteCallback)

Example 3 with FutureWriteCallback

use of org.eclipse.jetty.websocket.common.io.FutureWriteCallback 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);
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) FutureWriteCallback(org.eclipse.jetty.websocket.common.io.FutureWriteCallback) 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)

Aggregations

FutureWriteCallback (org.eclipse.jetty.websocket.common.io.FutureWriteCallback)3 URI (java.net.URI)1 ByteBuffer (java.nio.ByteBuffer)1 EncodeException (javax.websocket.EncodeException)1 Encoder (javax.websocket.Encoder)1 Session (org.eclipse.jetty.websocket.api.Session)1 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)1 MessageOutputStream (org.eclipse.jetty.websocket.common.message.MessageOutputStream)1 MessageWriter (org.eclipse.jetty.websocket.common.message.MessageWriter)1 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)1 EncodeFailedFuture (org.eclipse.jetty.websocket.jsr356.encoders.EncodeFailedFuture)1 Test (org.junit.Test)1