Netty 可以拆分消息中的字段吗?
Can Netty Split Fields Within Message?
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()
}
})
}
})
简短的回答是否定的...您可以做的是添加两个 DelimiterBasedFrameDecoder 解码器.. 第一个分割在“\r\n”上,第二个分割在“,”上。
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()
}
})
}
})
简短的回答是否定的...您可以做的是添加两个 DelimiterBasedFrameDecoder 解码器.. 第一个分割在“\r\n”上,第二个分割在“,”上。