use of io.netty.handler.codec.http.websocketx.TextWebSocketFrame in project netty-socketio by mrniko.
the class EncoderHandler method handleWebsocket.
private void handleWebsocket(final OutPacketMessage msg, ChannelHandlerContext ctx, ChannelPromise promise) throws IOException {
while (true) {
Queue<Packet> queue = msg.getClientHead().getPacketsQueue(msg.getTransport());
Packet packet = queue.poll();
if (packet == null) {
promise.trySuccess();
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()) {
if (!promise.isDone()) {
ctx.channel().writeAndFlush(res, promise);
} else {
ctx.channel().writeAndFlush(res);
}
} else {
promise.trySuccess();
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());
}
ctx.channel().writeAndFlush(new BinaryWebSocketFrame(outBuf));
}
}
}
use of io.netty.handler.codec.http.websocketx.TextWebSocketFrame 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().close();
ReferenceCountUtil.release(msg);
} 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.TextWebSocketFrame in project undertow by undertow-io.
the class AbstractWebSocketServerTest method testText.
@Test
public void testText() throws Exception {
if (getVersion() == WebSocketVersion.V00) {
// ignore 00 tests for now
return;
}
final AtomicBoolean connected = new AtomicBoolean(false);
DefaultServer.setRootHandler(new WebSocketProtocolHandshakeHandler(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 {
String string = message.getData();
if (string.equals("hello")) {
WebSockets.sendText("world", channel, null);
} else {
WebSockets.sendText(string, channel, null);
}
}
});
channel.resumeReceives();
}
}));
final FutureResult<?> latch = new FutureResult();
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.copiedBuffer("hello", CharsetUtil.US_ASCII)), new FrameChecker(TextWebSocketFrame.class, "world".getBytes(CharsetUtil.US_ASCII), latch));
latch.getIoFuture().get();
client.destroy();
}
use of io.netty.handler.codec.http.websocketx.TextWebSocketFrame in project undertow by undertow-io.
the class FrameChecker method onFrame.
@Override
public void onFrame(WebSocketFrame frame) {
try {
if (first) {
first = false;
Assert.assertTrue(clazz.isInstance(frame));
if (frame instanceof TextWebSocketFrame) {
String buf = ((TextWebSocketFrame) frame).text();
Assert.assertEquals(new String(expectedPayload, StandardCharsets.UTF_8), buf);
} else {
ByteBuf buf = frame.content();
byte[] data = new byte[buf.readableBytes()];
buf.readBytes(data);
Assert.assertArrayEquals(expectedPayload, data);
}
latch.setResult(null);
} else {
Assert.assertTrue(CloseWebSocketFrame.class.isInstance(frame));
}
} catch (Throwable e) {
latch.setException(new IOException(e));
}
}
use of io.netty.handler.codec.http.websocketx.TextWebSocketFrame in project undertow by undertow-io.
the class DynamicEndpointTest method testDynamicProgramaticEndpoint.
@Test
public void testDynamicProgramaticEndpoint() throws Exception {
final byte[] payload = "hello".getBytes();
final FutureResult latch = new FutureResult();
WebSocketTestClient client = new WebSocketTestClient(WebSocketVersion.V13, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/dynamicEchoEndpoint"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, "/dynamicEchoEndpoint hello".getBytes(), latch));
latch.getIoFuture().get();
client.destroy();
}
Aggregations