vince
2023-12-26 9a139b15be65dcd83ae187bb384627097c6a811a
src/main/java/com/fzzy/protocol/youxian0/client/ClientEngine.java
@@ -30,7 +30,7 @@
    private String host;
    private int port;
    public Channel defaultChannel;
    public  static  Channel defaultChannel;
    public ClientEngine(String host, int port) {
@@ -46,6 +46,7 @@
    @Override
    public void run() {
        try {
            startClient();
        } catch (Exception e) {
            e.printStackTrace();
@@ -53,6 +54,10 @@
    }
    public void startClient() throws Exception {
        if(defaultChannel != null){
            log.info("-----IP={},连接存在,直接使用",host);
            return;
        }
        EventLoopGroup group = new OioEventLoopGroup();
        Bootstrap b = new Bootstrap();
        //默认长连接
@@ -82,9 +87,9 @@
            @Override
            public void operationComplete(ChannelFuture arg0) throws Exception {
                if (channelFuture.isSuccess()) {
                    log.info("-----IP={},连接成功");
                    log.info("-----IP={},连接成功",host);
                } else {
                    log.info("-----IP={},连接失败,自动关闭线程");
                    log.info("-----IP={},连接失败,自动关闭线程",host);
                    channelFuture.cause().printStackTrace();
                    group.shutdownGracefully(); // 关闭线程组
                }
@@ -107,7 +112,7 @@
    }
    public Channel getChannel() {
    public static Channel getChannel() {
        return defaultChannel;
    }