Netty写了几条消息

时间:2014-11-30 17:08:07

标签: java netty channel

我遇到的问题与此处(Netty write several messages through one channel)相同,我无法解决。在这里你是一些案例:

  • 我可以从客户端发送多条消息,只有在没有响应的情况下服务器才会收到。
  • 如果我向服务器发送消息并且处理(使用处理程序),生成和writeAndFlush响应,则通道将切换为关闭,我无法从客户端发送更多消息。
  • 如果我使用write()方法从客户端发送消息,则消息不会到达服务器,因为需要刷新。它不会关闭频道。

在生成响应和writeAndFlush时,问题出现在服务器端。

我正在开发RTSP客户端/服务器并且此协议基于HTTP / 1.1遵循Snoop Http Server的Ne​​tty文档(http://netty.io/4.0/xref/io/netty/example/http/snoop/package-summary.html),如果您使用keep-alive选项,则消息只写和不冲洗。在我使用的情况下,我再说一遍,消息不会到达服务器。

这里有一些代码:

  

客户端

    // Bootstrap
    public RtspClient(String host, String port) throws NumberFormatException, InterruptedException{
                  // Configure the client.
                  EventLoopGroup group = new NioEventLoopGroup();
                  try {
                      b = new Bootstrap();
                      b.group(group)
                       .channel(NioSocketChannel.class)
                       .handler(new RtspClientInitializer());

                      ch = b.connect(host, Integer.parseInt(port)).sync().channel();

                 } finally {
                     // Shut down executor threads to exit.
                     // group.shutdownGracefully();
                 }
    }

     // Channel Pipeline definition
     class RtspClientInitializer extends ChannelInitializer<SocketChannel> {
        @Override
          public void initChannel(SocketChannel ch) {
              ChannelPipeline p = ch.pipeline();
              p.addLast("encoder", new RtspRequestEncoder());
              p.addLast("decoder", new RtspResponseDecoder());
              p.addLast("handler", new RtspClientHandler());
          }
      }

      // The method which sends RTSP OPTIONS protocol method
      public void sendOptions() throws InterruptedException{

        // HTTP/RTSP request.
        DefaultFullHttpRequest request = new DefaultFullHttpRequest(
        RtspVersions.RTSP_1_0, RtspMethods.OPTIONS, "*");
        request.headers().set(RtspHeaders.Names.CONNECTION, RtspHeaders.Values.KEEP_ALIVE);
        request.headers().set(RtspHeaders.Names.CONTENT_LENGTH, request.content().readableBytes());
        request.headers().set(RtspHeaders.Names.CSEQ, this.CSeq);
        this.CSeq++;

        // Write to channel

        System.out.println("Request: ------------------------------------------------------");
        System.out.println(request.toString());
        System.out.println("---------------------------------------------------------------");

        ch.writeAndFlush(request);

      }
  

服务器端

    // Bootstrap
    public void initRtspServer() throws InterruptedException{
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
             .channel(NioServerSocketChannel.class)
             .handler(new LoggingHandler(LogLevel.INFO)).childHandler(new RtspServerInitializer());

            ChannelFuture cf = b.bind(this.port);

            System.err.println("RTSP Server run at this URL: " +
                    "rtsp" + "://127.0.0.1:" + this.port + '/');

            cf.channel().closeFuture().sync();

    } finally {
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
    }


    // Channel Pipeline definition
    class RtspServerInitializer extends ChannelInitializer<SocketChannel>{

        @Override
        public void initChannel(SocketChannel ch) {
            ChannelPipeline p = ch.pipeline();
            p.addLast("decoder",new RtspRequestDecoder());
            p.addLast("encoder",new RtspResponseEncoder());
            p.addLast("handler",new RtspServerHandler());
        }
     }

     // In this case I only handle the OPTIONS method on server-side
     class RtspServerHandler extends SimpleChannelInboundHandler<HttpRequest> {

        private HttpRequest request;
        private FullHttpResponse response;

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, HttpRequest request)
                throws Exception {

        this.response = new DefaultFullHttpResponse(RTSP_1_0,OK);
            String sequence = request.headers().get(RtspHeaders.Names.CSEQ);
            this.response.headers().set(RtspHeaders.Names.CSEQ, sequence);
            this.response.headers().set(RtspHeaders.Names.PUBLIC, "OPTIONS,SETUP,TEARDOWN,PLAY");
            ctx.writeAndFlush(response).isSuccess(); // I think server closes the channel here

        }
    }

如果我在客户端的主类中执行以下操作:

        RtspClient client = new RtspClient("127.0.0.1","554");
        client.sendOptions();
        Thread.sleep(1000);
        client.sendOptions();

第二个sendOptions()什么都不做。

使用Netty 3.2我没有遇到这个问题,它与send()方法一起正常工作。

0 个答案:

没有答案