use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame in project reactor-netty by reactor.
the class WebsocketClientOperations method onInboundNext.
@Override
@SuppressWarnings("FutureReturnValueIgnored")
public void onInboundNext(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof FullHttpResponse) {
started = true;
channel().pipeline().remove(HttpObjectAggregator.class);
FullHttpResponse response = (FullHttpResponse) msg;
setNettyResponse(response);
if (notRedirected(response)) {
try {
handshaker.finishHandshake(channel(), response);
// This change is needed after the Netty change https://github.com/netty/netty/pull/11966
ctx.read();
listener().onStateChange(this, HttpClientState.RESPONSE_RECEIVED);
} catch (Exception e) {
onInboundError(e);
// "FutureReturnValueIgnored" this is deliberate
ctx.close();
} finally {
// Release unused content (101 status)
response.content().release();
}
} else {
response.content().release();
listener().onUncaughtException(this, redirecting);
}
return;
}
if (!this.proxyPing && msg instanceof PingWebSocketFrame) {
// "FutureReturnValueIgnored" this is deliberate
ctx.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content()));
ctx.read();
return;
}
if (msg instanceof CloseWebSocketFrame && ((CloseWebSocketFrame) msg).isFinalFragment()) {
if (log.isDebugEnabled()) {
log.debug(format(channel(), "CloseWebSocketFrame detected. Closing Websocket"));
}
CloseWebSocketFrame closeFrame = new CloseWebSocketFrame(true, ((CloseWebSocketFrame) msg).rsv(), ((CloseWebSocketFrame) msg).content());
if (closeFrame.statusCode() != -1) {
sendCloseNow(closeFrame);
} else {
sendCloseNow(closeFrame, WebSocketCloseStatus.EMPTY);
}
onInboundComplete();
} else if (msg != LastHttpContent.EMPTY_LAST_CONTENT) {
super.onInboundNext(ctx, msg);
}
}
use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame in project reactor-netty by reactor.
the class WebsocketTest method testIssue663_1.
@Test
void testIssue663_1() throws Exception {
AtomicBoolean incomingData = new AtomicBoolean();
CountDownLatch latch = new CountDownLatch(1);
disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((i, o) -> o.sendObject(Flux.just(new PingWebSocketFrame(), new CloseWebSocketFrame()).delayElements(Duration.ofMillis(100))).then(i.receiveFrames().doOnNext(f -> {
if (f instanceof PongWebSocketFrame) {
incomingData.set(true);
}
}).doOnComplete(latch::countDown).then()))).bindNow();
createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> in.receiveFrames()).subscribe();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(incomingData.get()).isTrue();
}
use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame in project reactor-netty by reactor.
the class WebsocketTest method testIssue663_2.
@Test
void testIssue663_2() throws Exception {
AtomicBoolean incomingData = new AtomicBoolean();
CountDownLatch latch = new CountDownLatch(1);
disposableServer = createServer().handle((req, resp) -> resp.sendWebsocket((i, o) -> o.sendObject(Flux.just(new PingWebSocketFrame(), new CloseWebSocketFrame()).delayElements(Duration.ofMillis(100))).then(i.receiveFrames().doOnNext(f -> incomingData.set(true)).doOnComplete(latch::countDown).then()))).bindNow();
createClient(disposableServer.port()).websocket(WebsocketClientSpec.builder().handlePing(true).build()).uri("/").handle((in, out) -> in.receiveFrames()).subscribe();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(incomingData.get()).isFalse();
}
use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame in project async-http-client by AsyncHttpClient.
the class NettyWebSocket method sendPing.
@Override
public WebSocket sendPing(final byte[] payload, final WebSocketWriteCompleteListener listener) {
final ChannelPromise channelPromise = channel.newPromise();
channelPromise.addListener(listener);
channel.writeAndFlush(new PingWebSocketFrame(wrappedBuffer(payload)), channelPromise);
return this;
}
use of io.netty.handler.codec.http.websocketx.PingWebSocketFrame 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);
}
}
Aggregations