首页 > 解决方案 > 使用netty发送多个RTSP消息

问题描述

我想创建一个 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();

}

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

标签: javanettyniortsprtsp-client

解决方案


您想要使用DefaultFullHttpRequest或者您需要DefaultHttpRequest使用LastHttpContent.


推荐阅读