I am writing a client using netty for handling custom protocol. I have defined a handler which extends SimpleChannelInboundHandler which handles both sending and receiving message.
public class ClientHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
log.info("Client received: " + ((ByteBuf)o).toString(CharsetUtil.UTF_8));
System.out.println("Client received: " + ((ByteBuf)o).toString(CharsetUtil.UTF_8));
}
@Override
public void channelActive(ChannelHandlerContext channelHandlerContext){
log.info("Client sent: $"+ new MessageRequest().toString() +"$");
channelHandlerContext.writeAndFlush(Unpooled.copiedBuffer((new MessageRequest().toString()), CharsetUtil.UTF_8));
}
@Override
public void exceptionCaught(ChannelHandlerContext channelHandlerContext, Throwable cause){
cause.printStackTrace();
channelHandlerContext.close();
}
}
This handler is able to print the response to the console. But since I am writing an client which will be used by another service, I need to send the response to the service which is calling my client.
Kindly help me in sending the response received to the calling service.