使用netty发送多个RTSP消息

时间:2018-12-03 05:56:31

标签: java netty nio rtsp rtsp-client

我想创建一个RTSP客户端,以发送一些RTSP消息。我使用netty编写代码,但是我的代码只能发送一个消息。如何发送其他消息? 我的客户代码如下:

public class RtspClient {
    public static class ClientHandler extends SimpleChannelInboundHandler<DefaultHttpResponse> {


        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) {
            ctx.flush();
        }


        protected void channelRead0(ChannelHandlerContext ctx, DefaultHttpResponse msg) throws Exception {
            System.out.println(msg.toString());
        }

    }


    public static void main(String[] args) throws InterruptedException {

        EventLoopGroup workerGroup = new NioEventLoopGroup();

        final ClientHandler handler = new ClientHandler();

        Bootstrap b = new Bootstrap();
        b.group(workerGroup);
        b.channel(NioSocketChannel.class);
        b.option(ChannelOption.SO_KEEPALIVE, true);
        b.remoteAddress("127.0.0.1", 8557);
        b.handler(new ChannelInitializer<SocketChannel>() {
            protected void initChannel(SocketChannel ch) {
                ChannelPipeline p = ch.pipeline();
                p.addLast("encoder", new RtspEncoder());
                p.addLast("decoder", new RtspDecoder());

                p.addLast(handler);
            }
        });

        Channel channel = b.connect().sync().channel();

        DefaultHttpRequest request = new DefaultHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.PLAY, "rtsp:123");
        request.headers().add(RtspHeaderNames.CSEQ, 1);
        request.headers().add(RtspHeaderNames.SESSION, "294");
        channel.writeAndFlush(request);

        Thread.sleep(10000);

         System.out.println(channel.isWritable());
         System.out.println(channel.isActive());
            request = new DefaultHttpRequest(RtspVersions.RTSP_1_0, RtspMethods.TEARDOWN, "rtsp3");
            request.headers().add(RtspHeaderNames.CSEQ, 2);
            request.headers().add(RtspHeaderNames.SESSION, "294");


        }
        channel.writeAndFlush(request);
        Scanner sc = new Scanner(System.in);
        sc.nextLine();
        channel.closeFuture().sync();

}

此代码只能发送第一条消息。服务器未收到第二个数据。如何发送其他消息?

1 个答案:

答案 0 :(得分:1)

您要使用DefaultFullHttpRequest,或者需要用DefaultHttpRequest“终止”每个LastHttpContent