使用Netty发送http响应
我正在尝试使用Netty编写一个RTSP服务器 现在,客户端发送请求使用Netty发送http响应,netty,rtsp,Netty,Rtsp,我正在尝试使用Netty编写一个RTSP服务器 现在,客户端发送请求 OPTIONS rtsp://localhost:8080 RTSP/1.0 CSeq: 2 User-Agent: LibVLC/2.2.4 (LIVE555 Streaming Media v2016.02.22) 我想把下面的回复发回去 RTSP/1.0 200 OK CSeq: 2 Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE 我应该使用什么来构造http响应。我应该
OPTIONS rtsp://localhost:8080 RTSP/1.0
CSeq: 2
User-Agent: LibVLC/2.2.4 (LIVE555 Streaming Media v2016.02.22)
我想把下面的回复发回去
RTSP/1.0 200 OK
CSeq: 2
Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE
我应该使用什么来构造http响应。我应该使用还是只使用普通字节数组并将其转换为ByteBuf
我使用的Netty版本是4.1.5
提前感谢。您希望将
FullHttpResponse
与管道中的Rtsp处理程序一起使用。选项请求的Rtsp响应仅包含标题
然后,您可以简单地创建响应并使用以下方法填充它:
FullHttpResponse response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
response.headers().add(RtspHeadersNames.PUBLIC, "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
response.headers().add(RtspHeadersNames.CSEQ, cseq);
响应选项请求的RTSP服务器的简化实现可以是:
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.*;
import io.netty.handler.codec.rtsp.*;
public class RtspServer {
public static class RtspServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelReadComplete(ChannelHandlerContext ctx) {
ctx.flush();
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
if (msg instanceof DefaultHttpRequest) {
DefaultHttpRequest req = (DefaultHttpRequest) msg;
FullHttpResponse response = new DefaultFullHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
response.headers().add(RtspHeadersNames.PUBLIC, "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE");
response.headers().add(RtspHeadersNames.CSEQ, req.headers().get("CSEQ"));
response.headers().set(RtspHeadersNames.CONNECTION, RtspHeadersValues.KEEP_ALIVE);
ctx.write(response);
}
}
}
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup);
b.channel(NioServerSocketChannel.class);
b.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) {
ChannelPipeline p = ch.pipeline();
p.addLast(new RtspDecoder(), new RtspEncoder());
p.addLast(new RtspServerHandler());
}
});
Channel ch = b.bind(8554).sync().channel();
System.err.println("Connect to rtsp://127.0.0.1:8554");
ch.closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
导入io.netty.bootstrap.ServerBootstrap;
导入io.netty.channel.*;
导入io.netty.channel.nio.NioEventLoopGroup;
导入io.netty.channel.socket.nio.NioServerSocketChannel;
导入io.netty.channel.socket.SocketChannel;
导入io.netty.handler.codec.http.*;
导入io.netty.handler.codec.rtsp.*;
公共类RtspServer{
公共静态类RTSPSServerHandler扩展了ChannelInboundHandlerAdapter{
@凌驾
公共无效channelReadComplete(ChannelHandlerContext ctx){
ctx.flush();
}
@凌驾
public void channelRead(ChannelHandlerContext ctx,Object msg){
如果(msg instanceof DefaultHttpRequest){
DefaultHttpRequest请求=(DefaultHttpRequest)消息;
FullHttpResponse response=新的默认FullHttpResponse(RtspVersions.RTSP_1_0,RtspResponseStatuses.OK);
response.headers().add(RtspHeadersNames.PUBLIC,“描述、设置、拆卸、播放、暂停”);
response.headers().add(RtspHeadersNames.CSEQ,req.headers().get(“CSEQ”);
response.headers().set(RtspHeadersNames.CONNECTION,RtspHeadersValues.KEEP_活动);
写(应答);
}
}
}
公共静态void main(字符串[]args)引发异常{
EventLoopGroup bossGroup=新的NioEventLoopGroup();
EventLoopGroup workerGroup=新的NioEventLoopGroup();
试一试{
ServerBootstrap b=新的ServerBootstrap();
b、 组(bossGroup、workerGroup);
b、 通道(NioServerSocketChannel.class);
b、 childHandler(新的ChannelInitializer(){
@凌驾
公共频道(SocketChannel ch){
ChannelPipeline p=通道管道();
p、 addLast(新的RtspDecoder(),新的RtspEncoder());
p、 addLast(新的RtspServerHandler());
}
});
通道ch=b.bind(8554).sync().Channel();
System.err.println(“连接到rtsp://127.0.0.1:8554");
ch.closeFuture().sync();
}最后{
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}