use of io.netty.handler.codec.http.websocketx.WebSocketFrame 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.WebSocketFrame in project netty by netty.
the class DeflateEncoder method encode.
@Override
protected void encode(ChannelHandlerContext ctx, WebSocketFrame msg, List<Object> out) throws Exception {
final ByteBuf compressedContent;
if (msg.content().isReadable()) {
compressedContent = compressContent(ctx, msg);
} else if (msg.isFinalFragment()) {
// Set empty DEFLATE block manually for unknown buffer size
// https://tools.ietf.org/html/rfc7692#section-7.2.3.6
compressedContent = EMPTY_DEFLATE_BLOCK.duplicate();
} else {
throw new CodecException("cannot compress content buffer");
}
final WebSocketFrame outMsg;
if (msg instanceof TextWebSocketFrame) {
outMsg = new TextWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
} else if (msg instanceof BinaryWebSocketFrame) {
outMsg = new BinaryWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
} else if (msg instanceof ContinuationWebSocketFrame) {
outMsg = new ContinuationWebSocketFrame(msg.isFinalFragment(), rsv(msg), compressedContent);
} else {
throw new CodecException("unexpected frame type: " + msg.getClass().getName());
}
out.add(outMsg);
}
use of io.netty.handler.codec.http.websocketx.WebSocketFrame in project autobahn-java by crossbario.
the class NettyWebSocket method send.
@Override
public void send(byte[] payload, boolean isBinary) {
WebSocketFrame frame;
if (isBinary) {
frame = new BinaryWebSocketFrame(toByteBuf(payload));
} else {
frame = new TextWebSocketFrame(toByteBuf(payload));
}
mChannel.writeAndFlush(frame);
}
use of io.netty.handler.codec.http.websocketx.WebSocketFrame in project reactor-netty by reactor.
the class WebsocketTest method testCloseWebSocketFrameSentByClient.
@Test
void testCloseWebSocketFrameSentByClient() {
disposableServer = createServer().handle((req, res) -> res.sendWebsocket((in, out) -> out.sendString(Mono.just("echo")).sendObject(new CloseWebSocketFrame()))).bindNow();
Mono<Void> response = createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> out.sendObject(in.receiveFrames().doOnNext(WebSocketFrame::retain).then())).next();
StepVerifier.create(response).expectComplete().verify(Duration.ofSeconds(30));
}
use of io.netty.handler.codec.http.websocketx.WebSocketFrame in project reactor-netty by reactor.
the class WebsocketTest method testIssue900_2.
@Test
void testIssue900_2() throws Exception {
AtomicReference<WebSocketCloseStatus> statusServer = new AtomicReference<>();
AtomicReference<String> incomingData = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
disposableServer = createServer().handle((req, res) -> res.sendWebsocket((in, out) -> {
in.receiveCloseStatus().doOnNext(o -> {
statusServer.set(o);
latch.countDown();
}).subscribe();
return out.sendObject(Flux.just(new TextWebSocketFrame("echo"), new CloseWebSocketFrame(1008, "something")).delayElements(Duration.ofMillis(100))).then(in.receiveFrames().doOnNext(o -> {
if (o instanceof TextWebSocketFrame) {
incomingData.set(((TextWebSocketFrame) o).text());
}
}).then());
})).bindNow();
createClient(disposableServer.port()).websocket().uri("/").handle((in, out) -> out.sendObject(in.receiveFrames().doOnNext(WebSocketFrame::retain))).subscribe();
assertThat(latch.await(30, TimeUnit.SECONDS)).isTrue();
assertThat(incomingData.get()).isNotNull().isEqualTo("echo");
assertThat(statusServer.get()).isNotNull().isEqualTo(new WebSocketCloseStatus(1008, "something"));
}
Aggregations