use of javax.websocket.SendResult 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 javax.websocket.SendResult in project jetty.project by eclipse.
the class ExtensionStackProcessingTest method testDeflateFrameExtension.
@Test
public void testDeflateFrameExtension() throws Exception {
assumeDeflateFrameAvailable();
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().extensions(Arrays.<Extension>asList(new JsrExtension("deflate-frame"))).build();
final String content = "deflate_me";
final CountDownLatch messageLatch = new CountDownLatch(1);
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
Session session = client.connectToServer(new EndpointAdapter() {
@Override
public void onMessage(String message) {
Assert.assertEquals(content, message);
messageLatch.countDown();
}
}, config, uri);
// Make sure everything is wired properly.
OutgoingFrames firstOut = ((JsrSession) session).getOutgoingHandler();
Assert.assertTrue(firstOut instanceof ExtensionStack);
ExtensionStack extensionStack = (ExtensionStack) firstOut;
Assert.assertTrue(extensionStack.isRunning());
OutgoingFrames secondOut = extensionStack.getNextOutgoing();
Assert.assertTrue(secondOut instanceof DeflateFrameExtension);
DeflateFrameExtension deflateExtension = (DeflateFrameExtension) secondOut;
Assert.assertTrue(deflateExtension.isRunning());
OutgoingFrames thirdOut = deflateExtension.getNextOutgoing();
Assert.assertTrue(thirdOut instanceof WebSocketClientConnection);
final CountDownLatch latch = new CountDownLatch(1);
session.getAsyncRemote().sendText(content, new SendHandler() {
@Override
public void onResult(SendResult result) {
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(messageLatch.await(5, TimeUnit.SECONDS));
}
use of javax.websocket.SendResult in project jetty.project by eclipse.
the class ExtensionStackProcessingTest method testPerMessageDeflateExtension.
@Test
public void testPerMessageDeflateExtension() throws Exception {
assumeDeflateFrameAvailable();
ClientEndpointConfig config = ClientEndpointConfig.Builder.create().extensions(Arrays.<Extension>asList(new JsrExtension("permessage-deflate"))).build();
final String content = "deflate_me";
final CountDownLatch messageLatch = new CountDownLatch(1);
URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
Session session = client.connectToServer(new EndpointAdapter() {
@Override
public void onMessage(String message) {
Assert.assertEquals(content, message);
messageLatch.countDown();
}
}, config, uri);
final CountDownLatch latch = new CountDownLatch(1);
session.getAsyncRemote().sendText(content, new SendHandler() {
@Override
public void onResult(SendResult result) {
latch.countDown();
}
});
Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
Assert.assertTrue(messageLatch.await(5, TimeUnit.SECONDS));
}
use of javax.websocket.SendResult in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testTextByCompletion.
@org.junit.Test
public void testTextByCompletion() throws Exception {
final byte[] payload = "payload".getBytes();
final AtomicReference<SendResult> sendResult = new AtomicReference<>();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult latch = new FutureResult();
final FutureResult latch2 = new FutureResult();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
session.getAsyncRemote().sendText(message, new SendHandler() {
@Override
public void onResult(SendResult result) {
sendResult.set(result);
if (result.getException() != null) {
latch2.setException(new IOException(result.getException()));
} else {
latch2.setResult(null);
}
}
});
}
});
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
latch.getIoFuture().get();
latch2.getIoFuture().get();
SendResult result = sendResult.get();
Assert.assertNotNull(result);
Assert.assertNull(result.getException());
client.destroy();
}
use of javax.websocket.SendResult in project undertow by undertow-io.
the class SendHandlerAdapter method onError.
@Override
public void onError(WebSocketChannel channel, Void context, Throwable throwable) {
if (done) {
return;
}
done = true;
handler.onResult(new SendResult(throwable));
}
Aggregations