use of io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame in project autobahn-java by crossbario.
the class NettyWebSocketClientHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel ch = ctx.channel();
if (!mHandshaker.isHandshakeComplete()) {
FullHttpResponse response = (FullHttpResponse) msg;
String negotiatedSerializer = response.headers().get("Sec-WebSocket-Protocol");
LOGGER.d(String.format("Negotiated serializer=%s", negotiatedSerializer));
ISerializer serializer = initializeSerializer(negotiatedSerializer);
mHandshaker.finishHandshake(ch, response);
mHandshakeFuture.setSuccess();
mTransportHandler.onConnect(mTransport, serializer);
} else if (msg instanceof FullHttpResponse) {
FullHttpResponse response = (FullHttpResponse) msg;
throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
} else if (msg instanceof BinaryWebSocketFrame) {
BinaryWebSocketFrame binaryWebSocketFrame = (BinaryWebSocketFrame) msg;
byte[] payload = new byte[binaryWebSocketFrame.content().readableBytes()];
LOGGER.v(String.format("Received binary frame, content length=%s", payload.length));
binaryWebSocketFrame.content().readBytes(payload);
mTransportHandler.onMessage(payload, true);
} else if (msg instanceof TextWebSocketFrame) {
TextWebSocketFrame textWebSocketFrame = (TextWebSocketFrame) msg;
byte[] payload = new byte[textWebSocketFrame.content().readableBytes()];
LOGGER.v(String.format("Received Text frame, content length=%s", payload.length));
textWebSocketFrame.content().readBytes(payload);
mTransportHandler.onMessage(payload, false);
} else if (msg instanceof PingWebSocketFrame) {
PingWebSocketFrame pingWebSocketFrame = (PingWebSocketFrame) msg;
ctx.writeAndFlush(new PongWebSocketFrame(pingWebSocketFrame.content().retain()));
} else if (msg instanceof PongWebSocketFrame) {
// Not really doing anything here.
LOGGER.v("WebSocket Client received pong.");
} else if (msg instanceof CloseWebSocketFrame) {
CloseWebSocketFrame closeWebSocketFrame = (CloseWebSocketFrame) msg;
LOGGER.d(String.format("Received Close frame, code=%s, reason=%s", closeWebSocketFrame.statusCode(), closeWebSocketFrame.reasonText()));
close(ctx, closeWebSocketFrame.statusCode() == 1000, new CloseDetails(CloseDetails.REASON_DEFAULT, null));
}
}
Aggregations