使用 Netty 发送 http 响应
Send http response using Netty
我正在尝试使用 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响应。我应该使用 HttpResponse 还是只使用普通字节数组并将其转换为 ByteBuf?
我使用的Netty版本是4.1.5
提前致谢。
您想将 FullHttpResponse
与管道中的 Rtsp 处理程序一起使用。
OPTIONS 请求的 RTSP 响应仅包含 headers.
然后您可以简单地创建响应并使用 :
填充它
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);
响应 OPTIONS 请求的 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();
}
}
}
我正在尝试使用 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响应。我应该使用 HttpResponse 还是只使用普通字节数组并将其转换为 ByteBuf?
我使用的Netty版本是4.1.5
提前致谢。
您想将 FullHttpResponse
与管道中的 Rtsp 处理程序一起使用。
OPTIONS 请求的 RTSP 响应仅包含 headers.
然后您可以简单地创建响应并使用 :
填充它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);
响应 OPTIONS 请求的 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();
}
}
}