Netty可以在消息中拆分字段吗?

时间:2015-02-02 07:18:16

标签: java netty

Netty可以将网络数据包拆分为单个消息,但它是否可以拆分消息中的字段?

message delimeter = "\r\n"
field delimiter = ","

example network packet = "A,B,C\r\nD,E,F\r\n"

示例代码:

val delimiterDecoder = new DelimiterBasedFrameDecoder(Integer.MAX_VALUE, Delimiters.lineDelimiter().apply(0))

val bootstrap = new Bootstrap

bootstrap.group(new OioEventLoopGroup())
  .channel(classOf[OioSocketChannel])
  .handler(new ChannelInitializer[SocketChannel]() {

  override def initChannel(channel: SocketChannel): Unit = {

    channel.pipeline().addLast(delimiterDecoder)

    channel.pipeline().addLast(new StringDecoder(CharsetUtil.US_ASCII))

    channel.pipeline().addLast(new SimpleChannelInboundHandler[String] {

      override def messageReceived(channelHandlerContext: ChannelHandlerContext, message: String): Unit = {

        // Can Netty do this instead of using Guava?
        val message = Splitter.on(',').split(buffer).iterator()

      }
    })
  }
})

1 个答案:

答案 0 :(得分:0)

简短的回答是否定的......您可以做的是添加两个DelimiterBasedFrameDecoder解码器..第一次拆分' \ r \ n'第二个是','。