use of org.xnio.FutureResult 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 org.xnio.FutureResult in project undertow by undertow-io.
the class ServletWebSocketHttpExchange method readRequestData.
@Override
public IoFuture<byte[]> readRequestData() {
final ByteArrayOutputStream data = new ByteArrayOutputStream();
try {
final ServletInputStream in = request.getInputStream();
byte[] buf = new byte[1024];
int r;
while ((r = in.read(buf)) != -1) {
data.write(buf, 0, r);
}
return new FinishedIoFuture<>(data.toByteArray());
} catch (IOException e) {
final FutureResult<byte[]> ioFuture = new FutureResult<>();
ioFuture.setException(e);
return ioFuture.getIoFuture();
}
}
use of org.xnio.FutureResult in project undertow by undertow-io.
the class WebSocketServletTest method testText.
@Test
public void testText() throws Exception {
final AtomicBoolean connected = new AtomicBoolean(false);
final ServletContainer container = ServletContainer.Factory.newInstance();
DeploymentUtils.setupServlet(new ServletInfo("websocket", WebSocketServlet.class, new ImmediateInstanceFactory<Servlet>(new WebSocketServlet(new WebSocketConnectionCallback() {
@Override
public void onConnect(final WebSocketHttpExchange exchange, final WebSocketChannel channel) {
connected.set(true);
channel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
final String string = message.getData();
if (string.equals("hello")) {
WebSockets.sendText("world", channel, null);
} else {
WebSockets.sendText(string, channel, null);
}
}
});
channel.resumeReceives();
}
}))).addMapping("/*"));
final FutureResult latch = new FutureResult();
WebSocketTestClient client = new WebSocketTestClient(io.netty.handler.codec.http.websocketx.WebSocketVersion.V13, new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/servletContext/"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(US_ASCII), latch));
latch.getIoFuture().get();
client.destroy();
}
Aggregations