use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project netty-socketio by mrniko.
the class WebSocketTransportTest method testCloseFrame.
/**
* Test method for {@link com.corundumstudio.socketio.transport.WebSocketTransport#channelRead()}.
*/
@Test
public void testCloseFrame() {
EmbeddedChannel channel = createChannel();
channel.writeInbound(new CloseWebSocketFrame());
Object msg = channel.readOutbound();
// https://tools.ietf.org/html/rfc6455#section-5.5.1
// If an endpoint receives a Close frame and did not previously send a Close frame, the endpoint
// MUST send a Close frame in response.
assertTrue(msg instanceof CloseWebSocketFrame);
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame 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());
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project netty by netty.
the class WebSocketExtensionFilterTest method testNeverSkip.
@Test
public void testNeverSkip() {
WebSocketExtensionFilter neverSkip = WebSocketExtensionFilter.NEVER_SKIP;
BinaryWebSocketFrame binaryFrame = new BinaryWebSocketFrame();
assertFalse(neverSkip.mustSkip(binaryFrame));
assertTrue(binaryFrame.release());
TextWebSocketFrame textFrame = new TextWebSocketFrame();
assertFalse(neverSkip.mustSkip(textFrame));
assertTrue(textFrame.release());
PingWebSocketFrame pingFrame = new PingWebSocketFrame();
assertFalse(neverSkip.mustSkip(pingFrame));
assertTrue(pingFrame.release());
PongWebSocketFrame pongFrame = new PongWebSocketFrame();
assertFalse(neverSkip.mustSkip(pongFrame));
assertTrue(pongFrame.release());
CloseWebSocketFrame closeFrame = new CloseWebSocketFrame();
assertFalse(neverSkip.mustSkip(closeFrame));
assertTrue(closeFrame.release());
ContinuationWebSocketFrame continuationFrame = new ContinuationWebSocketFrame();
assertFalse(neverSkip.mustSkip(continuationFrame));
assertTrue(continuationFrame.release());
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project netty by netty.
the class WebSocketClientHandler method channelRead0.
@Override
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel ch = ctx.channel();
if (!handshaker.isHandshakeComplete()) {
try {
handshaker.finishHandshake(ch, (FullHttpResponse) msg);
System.out.println("WebSocket Client connected!");
handshakeFuture.setSuccess();
} catch (WebSocketHandshakeException e) {
System.out.println("WebSocket Client failed to connect");
handshakeFuture.setFailure(e);
}
return;
}
if (msg instanceof FullHttpResponse) {
FullHttpResponse response = (FullHttpResponse) msg;
throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')');
}
WebSocketFrame frame = (WebSocketFrame) msg;
if (frame instanceof TextWebSocketFrame) {
TextWebSocketFrame textFrame = (TextWebSocketFrame) frame;
System.out.println("WebSocket Client received message: " + textFrame.text());
} else if (frame instanceof PongWebSocketFrame) {
System.out.println("WebSocket Client received pong");
} else if (frame instanceof CloseWebSocketFrame) {
System.out.println("WebSocket Client received closing");
ch.close();
}
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame 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