Search in sources :

Example 1 with SendHandlerWriteCallback

use of org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback in project jetty.project by eclipse.

the class JsrAsyncRemote method sendObject.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void sendObject(Object data, SendHandler handler) {
    assertMessageNotNull(data);
    assertSendHandlerNotNull(handler);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendObject({},{})", data, handler);
    }
    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 etxt = (Encoder.Text) encoder;
        try {
            String msg = etxt.encode(data);
            sendText(msg, handler);
            return;
        } catch (EncodeException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.TextStream) {
        Encoder.TextStream etxt = (Encoder.TextStream) encoder;
        SendHandlerWriteCallback callback = new SendHandlerWriteCallback(handler);
        try (MessageWriter writer = new MessageWriter(session)) {
            writer.setCallback(callback);
            etxt.encode(data, writer);
            return;
        } catch (EncodeException | IOException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.Binary) {
        Encoder.Binary ebin = (Encoder.Binary) encoder;
        try {
            ByteBuffer buf = ebin.encode(data);
            sendBinary(buf, handler);
            return;
        } catch (EncodeException e) {
            handler.onResult(new SendResult(e));
        }
    } else if (encoder instanceof Encoder.BinaryStream) {
        Encoder.BinaryStream ebin = (Encoder.BinaryStream) encoder;
        SendHandlerWriteCallback callback = new SendHandlerWriteCallback(handler);
        try (MessageOutputStream out = new MessageOutputStream(session)) {
            out.setCallback(callback);
            ebin.encode(data, out);
            return;
        } catch (EncodeException | IOException e) {
            handler.onResult(new SendResult(e));
        }
    }
    throw new IllegalArgumentException("Unknown encoder type: " + encoder);
}
Also used : EncodeException(javax.websocket.EncodeException) ByteBuffer(java.nio.ByteBuffer) SendHandlerWriteCallback(org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback) MessageOutputStream(org.eclipse.jetty.websocket.common.message.MessageOutputStream) Encoder(javax.websocket.Encoder) SendResult(javax.websocket.SendResult) MessageWriter(org.eclipse.jetty.websocket.common.message.MessageWriter)

Example 2 with SendHandlerWriteCallback

use of org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback in project jetty.project by eclipse.

the class JsrAsyncRemote method sendText.

@Override
public void sendText(String text, SendHandler handler) {
    assertMessageNotNull(text);
    assertSendHandlerNotNull(handler);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendText({},{})", TextUtil.hint(text), handler);
    }
    WebSocketFrame frame = new TextFrame().setPayload(text).setFin(true);
    jettyRemote.uncheckedSendFrame(frame, new SendHandlerWriteCallback(handler));
}
Also used : SendHandlerWriteCallback(org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame)

Example 3 with SendHandlerWriteCallback

use of org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback in project jetty.project by eclipse.

the class JsrAsyncRemote method sendBinary.

@Override
public void sendBinary(ByteBuffer data, SendHandler handler) {
    assertMessageNotNull(data);
    assertSendHandlerNotNull(handler);
    if (LOG.isDebugEnabled()) {
        LOG.debug("sendBinary({},{})", BufferUtil.toDetailString(data), handler);
    }
    WebSocketFrame frame = new BinaryFrame().setPayload(data).setFin(true);
    jettyRemote.uncheckedSendFrame(frame, new SendHandlerWriteCallback(handler));
}
Also used : SendHandlerWriteCallback(org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback) BinaryFrame(org.eclipse.jetty.websocket.common.frames.BinaryFrame) WebSocketFrame(org.eclipse.jetty.websocket.common.WebSocketFrame)

Aggregations

SendHandlerWriteCallback (org.eclipse.jetty.websocket.jsr356.messages.SendHandlerWriteCallback)3 WebSocketFrame (org.eclipse.jetty.websocket.common.WebSocketFrame)2 ByteBuffer (java.nio.ByteBuffer)1 EncodeException (javax.websocket.EncodeException)1 Encoder (javax.websocket.Encoder)1 SendResult (javax.websocket.SendResult)1 BinaryFrame (org.eclipse.jetty.websocket.common.frames.BinaryFrame)1 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)1 MessageOutputStream (org.eclipse.jetty.websocket.common.message.MessageOutputStream)1 MessageWriter (org.eclipse.jetty.websocket.common.message.MessageWriter)1