使用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();
}

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

您想要使用DefaultFullHttpRequest,或者您需要用LastHttpContent"终止"每个DefaultHttpRequest

相关内容

  • 没有找到相关文章

最新更新