Netty client, how to get a response from a request sent to the server

I am trying to combine a Netty client. I need to send a request to the server (which I do not control), and I am waiting for a response.

I'm not quite sure how to return the answer.

So, if I had:

ChannelFuture future = bootstrap.connect(new InetSocketAddress("foo.com", 1654));
Channel connector = future.awaitUninterruptibly().getChannel();
ChannelFuture response = connector.write(query);

How to get response data from a ChannelFuture response? Do I need to add ChannelHandler to the bootstrap pipeline? If so, how do I link the response to the request?

Thank,

+5
source share
2 answers

, ChannelHandler. - SimpleChannelUpstreamHandler channelConnected(…) messageReceived(…). channelConnected Channel.write(…), messageReceived . , .

SimpleChannelUpstreamHandler . - :

channel.getPipeline().addLast("yourHandlerName", new SimpleChannelUpstreamHandler()) {
    public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
        // remove handler after receiving response
        ctx.getPipeline().remove(this);

         // do logic
         ...
      }
});
+2

-, localtime. "LocalTimeClientHandler" /.

" ", 2 , :

  • , . , , .

  • , , .

+2

All Articles