@Override public Future<Void> sendPingFrame() { return channel.writeAndFlush(new PingWebSocketFrame()); }
private void onPingFrame(PingWebSocketFrame frame) { byte[] bytes = byteBuf2Bytes(frame.content()); for (WebSocketListener listener : listeners) { listener.onPingFrame(bytes); } }
@Override public PingWebSocketFrame replace(ByteBuf content) { return new PingWebSocketFrame(isFinalFragment(), rsv(), content); }
@Override protected void channelRead0(ChannelHandlerContext context, Object message) throws Exception { final Channel channel = context.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(channel, (FullHttpResponse) message); channel.pipeline().addBefore(HANDLER_NAME, "websocket-frame-aggregator", new WebSocketFrameAggregator(64 * 1024)); subscriber.onStart(); return; } if (message instanceof FullHttpResponse) { final FullHttpResponse response = (FullHttpResponse) message; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } final WebSocketFrame frame = (WebSocketFrame) message; if (frame instanceof PingWebSocketFrame) { context.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame)frame).retain().content())); } else if (frame instanceof BinaryWebSocketFrame) { final ByteBufInputStream input = new ByteBufInputStream(((BinaryWebSocketFrame)message).content()); final Envelope envelope = Envelope.ADAPTER.decode(input); subscriber.onNext(envelope); } }
@Override public Future<Void> sendPingFrame(ByteBuf payload) { return channel.writeAndFlush(new PingWebSocketFrame(payload)); }
@Override public PingWebSocketFrame replace(ByteBuf content) { return new PingWebSocketFrame(isFinalFragment(), rsv(), content); }
@Override public ChannelFuture sendPing(ChannelHandlerContext ctx) { return ctx.channel().writeAndFlush(new PingWebSocketFrame()); }
@Override public PingWebSocketFrame replace(ByteBuf content) { return new PingWebSocketFrame(isFinalFragment(), rsv(), content); }
ctx.writeAndFlush(new PongWebSocketFrame(frame.content()));
@Override public ChannelFuture sendPing(ChannelHandlerContext ctx) { return ctx.channel().writeAndFlush(new PingWebSocketFrame()); }
@Override public PingWebSocketFrame replace(ByteBuf content) { return new PingWebSocketFrame(isFinalFragment(), rsv(), content); }
@Override protected void encode(ChannelHandlerContext ctx, WebSocketConvertHandler.MyWebSocketFrame msg, List<Object> out) throws Exception { ByteBuf payload = msg.getData().duplicate().retain(); switch (msg.getType()) { case BINARY: out.add(new BinaryWebSocketFrame(payload)); break; case TEXT: out.add(new TextWebSocketFrame(payload)); break; case CLOSE: out.add(new CloseWebSocketFrame(true, 0, payload)); break; case CONTINUATION: out.add(new ContinuationWebSocketFrame(payload)); break; case PONG: out.add(new PongWebSocketFrame(payload)); break; case PING: out.add(new PingWebSocketFrame(payload)); break; default: throw new IllegalStateException( "Unsupported websocket msg " + msg);} }
@Override public PingWebSocketFrame replace(ByteBuf content) { return new PingWebSocketFrame(isFinalFragment(), rsv(), content); }
@Override public void onPing(ChannelHandlerContext ctx, PingWebSocketFrame frame) { System.out.println("PING:" + frame.content().toString(Charset.forName("utf8"))); }
WebSocketFrame encodeFrame(WebSocketFrameImpl frame) { ByteBuf buf = frame.getBinaryData(); if (buf != Unpooled.EMPTY_BUFFER) { buf = safeBuffer(buf, chctx.alloc()); } switch (frame.type()) { case BINARY: return new BinaryWebSocketFrame(frame.isFinal(), 0, buf); case TEXT: return new TextWebSocketFrame(frame.isFinal(), 0, buf); case CLOSE: return new CloseWebSocketFrame(true, 0, buf); case CONTINUATION: return new ContinuationWebSocketFrame(frame.isFinal(), 0, buf); case PONG: return new PongWebSocketFrame(buf); case PING: return new PingWebSocketFrame(buf); default: throw new IllegalStateException("Unsupported websocket msg " + frame); } }
private void notifyPingMessage(PingWebSocketFrame pingWebSocketFrame) throws ServerConnectorException { //Control message for WebSocket is Ping Message ByteBuf byteBuf = pingWebSocketFrame.content(); ByteBuffer byteBuffer = byteBuf.nioBuffer(); WebSocketMessageImpl webSocketControlMessage = new WebSocketControlMessageImpl(WebSocketControlSignal.PING, byteBuffer); webSocketControlMessage = setupCommonProperties(webSocketControlMessage); connectorFuture.notifyWSListener((WebSocketControlMessage) webSocketControlMessage); }
protected WebSocketFrame toFrame(WebSocketMessage message) { ByteBuf byteBuf = NettyDataBufferFactory.toByteBuf(message.getPayload()); if (WebSocketMessage.Type.TEXT.equals(message.getType())) { return new TextWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.BINARY.equals(message.getType())) { return new BinaryWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PING.equals(message.getType())) { return new PingWebSocketFrame(byteBuf); } else if (WebSocketMessage.Type.PONG.equals(message.getType())) { return new PongWebSocketFrame(byteBuf); } else { throw new IllegalArgumentException("Unexpected message type: " + message.getType()); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object frame) throws Exception { if (CloseWebSocketFrame.class.equals(frame.getClass())) { handshaker.close(ctx.channel(), ((CloseWebSocketFrame) frame).retain()); downstream().complete(); return; } if (PingWebSocketFrame.class.isAssignableFrom(frame.getClass())) { ctx.channel().writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame)frame) .content().retain())); return; } doRead(frame); }