java - 使用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
或者您需要DefaultHttpRequest
使用LastHttpContent
.
推荐阅读
- c++ - 当您可以使用构造函数时,为什么要在 C++ 类或结构中重载 () 运算符(可调用运算符)?
- .net - dev-certs 生成没有根的证书
- html - 提供指向背景 url html 的链接
- android - RecyclerView的适配器,哪种形式的设置比较好?
- c# - Dispose() 方法中是否需要调用“Clear()”?
- c - 为什么一个字符串在 C 中写入两个字符?
- python - 如果 slug 已存在于 postgresql 数据库中,则跳过保存行 - python
- java - 为什么我的堆栈是空的?空堆栈异常
- php - 为什么 Ajax 仅在禁用 Internet 时工作?
- c# - 如何序列化/反序列化分布在程序集中的多个相同类实例