这是我的客户端的Handler代码
class RobotHandler : ChannelInboundHandlerAdapter() {
private val logger = LoggerFactory.getLogger(this.javaClass)
override fun channelRead(ctx: ChannelHandlerContext?, msg: Any?) {
logger.info(msg?.toString())
}
override fun channelActive(ctx: ChannelHandlerContext?) {
val request = DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/robot")
ctx?.writeAndFlush(request)
}
override fun exceptionCaught(ctx: ChannelHandlerContext?, cause: Throwable?) {
cause?.printStackTrace()
ctx?.close()
}
}
这是客户端的代码
@Component
@Order(1)
class RobotNettyClient : ApplicationRunner{
private val logger = LoggerFactory.getLogger(this.javaClass)
override fun run(args: ApplicationArguments?) {
val group = NioEventLoopGroup()
try {
val bootstrap = Bootstrap()
bootstrap.group(group)
.channel(NioSocketChannel::class.java)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(object: ChannelInitializer<SocketChannel>() {
override fun initChannel(ch: SocketChannel?) {
val pipeline = ch?.pipeline()
pipeline?.addLast(HttpClientCodec())
pipeline?.addLast(HttpContentCompressor())
pipeline?.addLast(RobotHandler())
}
})
logger.info("Netty started")
bootstrap.connect("127.0.0.1",8080).sync()
} finally {
group.shutdownGracefully()
}
}
}
我的服务器选用的是Spring boot,像在内部通过Netty实现数据不断读取和发送响应,然后获取到数据。可是我通过Netty向Spring boot发送响应过后,触发Controller但是无法接收到返回的消息,请问怎么解决