use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testBinaryWithByteBufferByFuture.
@org.junit.Test
public void testBinaryWithByteBufferByFuture() throws Exception {
final byte[] payload = "payload".getBytes();
final AtomicReference<Future<Void>> sendResult = new AtomicReference<>();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult<?> latch = new FutureResult<>();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
@Override
public void onMessage(ByteBuffer message) {
ByteBuffer buf = ByteBuffer.allocate(message.remaining());
buf.put(message);
buf.flip();
sendResult.set(session.getAsyncRemote().sendBinary(buf));
}
});
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), 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 BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
// FIXME UNDERTOW-1862 assertEquals(DONE, latch.getIoFuture().await());
latch.getIoFuture().get();
Future<Void> result = sendResult.get();
/* FIXME result.wait(4000);
assertTrue(result.isDone());*/
client.destroy();
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project vert.x by eclipse.
the class Http1xConnectionBase method decodeFrame.
private WebSocketFrameInternal decodeFrame(io.netty.handler.codec.http.websocketx.WebSocketFrame msg) {
ByteBuf payload = safeBuffer(msg.content());
boolean isFinal = msg.isFinalFragment();
WebSocketFrameType frameType;
if (msg instanceof BinaryWebSocketFrame) {
frameType = WebSocketFrameType.BINARY;
} else if (msg instanceof CloseWebSocketFrame) {
frameType = WebSocketFrameType.CLOSE;
} else if (msg instanceof PingWebSocketFrame) {
frameType = WebSocketFrameType.PING;
} else if (msg instanceof PongWebSocketFrame) {
frameType = WebSocketFrameType.PONG;
} else if (msg instanceof TextWebSocketFrame) {
frameType = WebSocketFrameType.TEXT;
} else if (msg instanceof ContinuationWebSocketFrame) {
frameType = WebSocketFrameType.CONTINUATION;
} else {
throw new IllegalStateException("Unsupported WebSocket msg " + msg);
}
return new WebSocketFrameImpl(frameType, payload, isFinal);
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty-socketio by mrniko.
the class WebSocketTransport method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof CloseWebSocketFrame) {
ctx.channel().writeAndFlush(msg).addListener(ChannelFutureListener.CLOSE);
} else if (msg instanceof BinaryWebSocketFrame || msg instanceof TextWebSocketFrame) {
ByteBufHolder frame = (ByteBufHolder) msg;
ClientHead client = clientsBox.get(ctx.channel());
if (client == null) {
log.debug("Client with was already disconnected. Channel closed!");
ctx.channel().close();
frame.release();
return;
}
ctx.pipeline().fireChannelRead(new PacketsMessage(client, frame.content(), Transport.WEBSOCKET));
frame.release();
} else if (msg instanceof FullHttpRequest) {
FullHttpRequest req = (FullHttpRequest) msg;
QueryStringDecoder queryDecoder = new QueryStringDecoder(req.uri());
String path = queryDecoder.path();
List<String> transport = queryDecoder.parameters().get("transport");
List<String> sid = queryDecoder.parameters().get("sid");
if (transport != null && NAME.equals(transport.get(0))) {
try {
if (!configuration.getTransports().contains(Transport.WEBSOCKET)) {
log.debug("{} transport not supported by configuration.", Transport.WEBSOCKET);
ctx.channel().close();
return;
}
if (sid != null && sid.get(0) != null) {
final UUID sessionId = UUID.fromString(sid.get(0));
handshake(ctx, sessionId, path, req);
} else {
ClientHead client = ctx.channel().attr(ClientHead.CLIENT).get();
// first connection
handshake(ctx, client.getSessionId(), path, req);
}
} finally {
req.release();
}
} else {
ctx.fireChannelRead(msg);
}
} else {
ctx.fireChannelRead(msg);
}
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty-socketio by mrniko.
the class EncoderHandler method handleWebsocket.
private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) throws IOException {
ChannelFutureList writeFutureList = new ChannelFutureList();
while (true) {
Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport());
Packet packet = queue.poll();
if (packet == null) {
writeFutureList.setChannelPromise(promise);
break;
}
final ByteBuf out = encoder.allocateBuffer(ctx.alloc());
encoder.encodePacket(packet, out, ctx.alloc(), true);
WebSocketFrame res = new TextWebSocketFrame(out);
if (log.isTraceEnabled()) {
log.trace("Out message: {} sessionId: {}", out.toString(CharsetUtil.UTF_8), msg.getSessionId());
}
if (out.isReadable()) {
writeFutureList.add(ctx.channel().writeAndFlush(res));
} else {
out.release();
}
for (ByteBuf buf : packet.getAttachments()) {
ByteBuf outBuf = encoder.allocateBuffer(ctx.alloc());
outBuf.writeByte(4);
outBuf.writeBytes(buf);
if (log.isTraceEnabled()) {
log.trace("Out attachment: {} sessionId: {}", ByteBufUtil.hexDump(outBuf), msg.getSessionId());
}
writeFutureList.add(ctx.channel().writeAndFlush(new BinaryWebSocketFrame(outBuf)));
}
}
}
use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project netty by netty.
the class WebSocketExtensionFilterTest method testAlwaysSkip.
@Test
public void testAlwaysSkip() {
WebSocketExtensionFilter neverSkip = WebSocketExtensionFilter.ALWAYS_SKIP;
BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame();
assertTrue(neverSkip.mustSkip(binaryFrame));
assertTrue(binaryFrame.release());
TextWebSocketFrame textFrame = new TextWebSocketFrame();
assertTrue(neverSkip.mustSkip(textFrame));
assertTrue(textFrame.release());
PingWebSocketFrame pingFrame = new PingWebSocketFrame();
assertTrue(neverSkip.mustSkip(pingFrame));
assertTrue(pingFrame.release());
PongWebSocketFrame pongFrame = new PongWebSocketFrame();
assertTrue(neverSkip.mustSkip(pongFrame));
assertTrue(pongFrame.release());
CloseWebSocketFrame closeFrame = new CloseWebSocketFrame();
assertTrue(neverSkip.mustSkip(closeFrame));
assertTrue(closeFrame.release());
ContinuationWebSocketFrame continuationFrame = new ContinuationWebSocketFrame();
assertTrue(neverSkip.mustSkip(continuationFrame));
assertTrue(continuationFrame.release());
}
Aggregations