use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project vert.x by eclipse.
the class WebSocketImplBase method handleCloseFrame.
private void handleCloseFrame(WebSocketFrameInternal closeFrame) {
boolean echo;
synchronized (conn) {
echo = closeStatusCode == null;
closed = true;
closeStatusCode = closeFrame.closeStatusCode();
closeReason = closeFrame.closeReason();
}
handleClose(true);
if (echo) {
ChannelPromise fut = conn.channelFuture();
conn.writeToChannel(new CloseWebSocketFrame(closeStatusCode, closeReason), fut);
fut.addListener(v -> handleCloseConnection());
} else {
handleCloseConnection();
}
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project vert.x by eclipse.
the class WebSocketTest method testServerCloseHandshake.
@Test
public void testServerCloseHandshake() {
short status = (short) (4000 + TestUtils.randomPositiveInt() % 100);
waitFor(2);
server = vertx.createHttpServer();
server.webSocketHandler(ws -> {
ws.closeHandler(sc -> {
assertEquals((Short) (short) status, ws.closeStatusCode());
complete();
});
});
server.listen(DEFAULT_HTTP_PORT, DEFAULT_HTTP_HOST, onSuccess(v1 -> {
client = vertx.createHttpClient();
handshake(client, req -> {
req.send(onSuccess(resp -> {
assertEquals(101, resp.statusCode());
Http1xClientConnection conn = (Http1xClientConnection) req.connection();
NetSocketInternal soi = conn.toNetSocket();
soi.channelHandlerContext().pipeline().addBefore("handler", "encoder", new WebSocket13FrameEncoder(true));
soi.channelHandlerContext().pipeline().addBefore("handler", "decoder", new WebSocket13FrameDecoder(false, false, 1000));
String reason = randomAlphaString(10);
soi.writeMessage(new CloseWebSocketFrame(status, reason));
AtomicBoolean closeFrameReceived = new AtomicBoolean();
soi.messageHandler(msg -> {
try {
if (msg instanceof CloseWebSocketFrame) {
CloseWebSocketFrame frame = (CloseWebSocketFrame) msg;
assertEquals(status, frame.statusCode());
assertEquals(reason, frame.reasonText());
closeFrameReceived.set(true);
}
} finally {
ReferenceCountUtil.release(msg);
}
});
soi.closeHandler(v2 -> {
assertTrue(closeFrameReceived.get());
complete();
});
}));
});
}));
await();
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project async-http-client by AsyncHttpClient.
the class NettyWebSocket method handleFrame.
public void handleFrame(WebSocketFrame frame) {
if (frame instanceof TextWebSocketFrame) {
onTextFrame((TextWebSocketFrame) frame);
} else if (frame instanceof BinaryWebSocketFrame) {
onBinaryFrame((BinaryWebSocketFrame) frame);
} else if (frame instanceof CloseWebSocketFrame) {
Channels.setDiscard(channel);
CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame;
onClose(closeFrame.statusCode(), closeFrame.reasonText());
Channels.silentlyCloseChannel(channel);
} else if (frame instanceof PingWebSocketFrame) {
onPing((PingWebSocketFrame) frame);
} else if (frame instanceof PongWebSocketFrame) {
onPong((PongWebSocketFrame) frame);
}
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project reactor-netty by reactor.
the class HttpServerWSOperations method onInboundNext.
@Override
public void onInboundNext(ChannelHandlerContext ctx, Object frame) {
if (frame instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) frame).isFinalFragment()) {
if (log.isDebugEnabled()) {
log.debug("CloseWebSocketFrame detected. Closing Websocket");
}
CloseWebSocketFrame close = (CloseWebSocketFrame) frame;
sendClose(new CloseWebSocketFrame(true, close.rsv(), close.content()), f -> onHandlerTerminate());
return;
}
if (frame instanceof PingWebSocketFrame) {
ctx.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) frame).content()));
ctx.read();
return;
}
super.onInboundNext(ctx, frame);
}
use of io.netty.handler.codec.http.websocketx.CloseWebSocketFrame in project undertow by undertow-io.
the class WebSocketTestClient method destroy.
/**
* Destroy the client and also close open connections if any exist
*/
public void destroy() {
if (!closed) {
final CountDownLatch latch = new CountDownLatch(1);
send(new CloseWebSocketFrame(), new FrameListener() {
@Override
public void onFrame(WebSocketFrame frame) {
latch.countDown();
}
@Override
public void onError(Throwable t) {
latch.countDown();
}
});
try {
latch.await(10, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// bootstrap.releaseExternalResources();
if (ch != null) {
ch.close().syncUninterruptibly();
}
try {
bootstrap.group().shutdownGracefully(0, 1, TimeUnit.SECONDS).get();
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
}
}
Aggregations