TcpServer.java 2.95 KB
package com.huaheng.pc.config.sn.tcp;

import com.baomidou.mybatisplus.extension.api.R;
import com.huaheng.common.utils.AddressUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
@Data
public class TcpServer implements CommandLineRunner {
    @Resource
    private AddressUtils addressUtils;

    @Override
    public void run(String... args) throws Exception {
        Integer port =10000;
        if(!addressUtils.isAlllowPort()){
            port=20000;
        }
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap bootstrap = new ServerBootstrap();
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<Channel>() {
                        @Override
                        protected void initChannel(Channel channel) throws Exception {
                            ChannelPipeline pipeline = channel.pipeline();
                            pipeline.addLast(new StringEncoder());
                            pipeline.addLast(new StringDecoder());
                            pipeline.addLast(new TCPServerHandler());
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);

            ChannelFuture future = bootstrap.bind(port).sync();
            log.info("TCP server started and listening on port " + port);

            future.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public AjaxResult clientList(){
        System.out.println(TCPServerHandler.channelGroup);
        TCPServerHandler.channelGroup.forEach(ch->{
            System.out.println("客户端ip及port:"+ch.remoteAddress());
        });
        return AjaxResult.success().setData(TCPServerHandler.channelGroup.size());
    }

    public AjaxResult sendToMsg(String clientIpPort,String sendMsg){
        TCPServerHandler.channelGroup.forEach(ch->{
            System.out.println("客户端ip及port:"+ch.remoteAddress());
            if(clientIpPort.equals(ch.remoteAddress().toString())){
                ch.writeAndFlush(sendMsg);
            }
        });
        return AjaxResult.success();
    }
}