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