问答

Netty http客户端无法接收到服务器的响应信息

作者:admin 2021-08-20 我要评论

这是我的客户端的Handler代码 class RobotHandler : ChannelInboundHandlerAdapter() { private val logger = LoggerFactory.getLogger(this.javaClass) overrid...

在说正事之前,我要推荐一个福利:你还在原价购买阿里云、腾讯云、华为云服务器吗?那太亏啦!来这里,新购、升级、续费都打折,能够为您省60%的钱呢!2核4G企业级云服务器低至69元/年,点击进去看看吧>>>)

这是我的客户端的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但是无法接收到返回的消息,请问怎么解决

版权声明:本文转载自网络,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。本站转载出于传播更多优秀技术知识之目的,如有侵权请联系QQ/微信:153890879删除

相关文章
  • 使用箭头函数return数据不能实现

    使用箭头函数return数据不能实现

  • 个人小程序号能接入小程序3D地图吗?

    个人小程序号能接入小程序3D地图吗?

  • vue 移动端项目中,使用了better-scrol

    vue 移动端项目中,使用了better-scrol

  • css如何做出区域平均划分为四列

    css如何做出区域平均划分为四列

腾讯云代理商
海外云服务器