2016-10-12 10 views
1

Netty를 사용하여 RTSP 서버를 작성하려고합니다.Netty를 사용하여 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 응답을 구성하려면 어떻게 사용해야 다음과 같은 응답을 보내려고합니다. HttpResponse을 사용하거나 일반 바이트 배열을 사용하여 ByteBuf로 변환해야합니까?

사용중인 Netty 버전은 4.15입니다.

미리 감사드립니다.

답변

1

OPTIONS 요청의 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(); 
     }  
    } 
} 
0

FullHttpResponse을 파이프 라인의 Rtsp 처리기와 함께 사용하려고합니다.

관련 문제