【发布时间】:2018-04-11 07:16:11
【问题描述】:
我正在通过 TCP 以标准 xml 格式获取消息,我正在通过我的 netty 客户端收听这些消息。应该如何编写我的管道,以便我可以解析以 <abc>....</abc> 开头和结尾的字符串?
我猜应该有一个字符串解码器和另一个处理从 TCP 流解析 XML 的自定义处理程序(netty 定义或其他方式)。
类似于以下内容?
public Bootstrap createBootstrap(final Bootstrap b, EventLoopGroup eventLoop) {
b.group(workerGroup);
b.channel(NioSocketChannel.class);
ReconnectionClient reconnectionClient = new ReconnectionClient(this);
b.handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(reconnectionClient);
pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
pipeline.addLast(new ClientHandler()); // deals with String parsing
}
});
// Use command line arguments to pass the socket
b.connect(serverIP, serverPort).addListener(new ConnectionListener(this));
return b;
}
我的问题的第二部分是如何解析通过 TCP 传输的单个 XML 字符串?例如,应该在 ClientHandler() 中编写什么代码来实现此功能?
【问题讨论】: