zhitong.yu
2024-10-11 4f58a93c95ff123d51adcb8fa2e521333e8ab022
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.hxzk.util.Netty;
 
 
 
import com.hxzk.service.CabinetService;
import com.hxzk.service.FaKaService;
import com.hxzk.service.PersonService;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import org.springframework.stereotype.Component;
 
import java.util.Stack;
import java.util.concurrent.TimeUnit;
 
 
@Component
public class NettyServer {
 
    static ChannelFuture cf = null;
 
    static PersonService personService;
    static CabinetService cabinetService;
 
    static FaKaService faKaService;
    public NettyServer(PersonService personService1,CabinetService cabinetService1,FaKaService faKaService1) {
        this.personService = personService1;
        this.cabinetService =cabinetService1;
        this.faKaService = faKaService1;
    }
 
    public NettyServer() {
    }
 
    public static void server(int port) {
 
        System.out.println("开始监听"+port);
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
 
        try {
            //创建服务器端的启动对象,配置参数
            ServerBootstrap bootstrap = new ServerBootstrap();
 
            //使用链式编程来进行设置
            bootstrap.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 1024)
                    .childOption(ChannelOption.SO_KEEPALIVE, true) //设置保持活动连接状态
 
 
                    .childHandler(new ChannelInitializer<SocketChannel>() {// 最后绑定I/O事件的处理类
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
                            /**
                             * readerIdleTime 多长时间没有读,就会发送一个心跳检测包检测是否连接
                             * writerIdleTime 多长时间没有写,就会发送一个心跳检测包检测是否连接
                             * allIdleTime 多长时间没有读/写,就会发送一个心跳检测包检测是否连接
                             * */
                            pipeline.addLast("decoder", new StringEncoder());
                            pipeline.addLast("encoder", new StringEncoder());
                            pipeline.addLast(new IdleStateHandler(60, 120, 120, TimeUnit.SECONDS));
                            pipeline.addLast(new NettyServerHandler(personService,cabinetService,faKaService));
                            pipeline.addLast(new GroupChatServerHandler());
 
                        }
                    });
 
            cf = bootstrap.bind(port).sync();
            cf.channel().closeFuture().addListener(new ChannelFutureListener() {
                public void operationComplete(ChannelFuture future) throws Exception {
                    cf.channel().close();
                }
            });
 
 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
 
 
 
}