Newer
Older
casic-robot-inspection / casic-server / src / main / java / com / casic / missiles / netty / NettyClientHandler.java
casic_zt on 10 Nov 2023 1 KB 中子源协议设备调试修改
package com.casic.missiles.netty;


import com.casic.missiles.neutron.service.INeutronOptService;
import com.casic.missiles.neutron.service.impl.NeutronOptServiceImpl;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ChannelHandler.Sharable
@Slf4j
public class NettyClientHandler extends ChannelInboundHandlerAdapter {

    @Autowired
    private INeutronOptService neutronOptService;

    // 服务器端读取到 客户端发送过来的数据,然后通过 Channel 回写数据
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        log.info(String.format("客户端读取到从服务器端:%s 发送过来的数据:%s", ctx.channel().remoteAddress(), msg.toString()));
//        ctx.channel().writeAndFlush(String.format("server write:%s", msg));
        //解析数据
        try {
            neutronOptService.analysis(msg.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    // 捕获到异常的处理
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

}