feiyu02
2025-09-30 a3cc1d220f8a1de11874bebceba0130d32157ff1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package cn.flightfeather.supervision.infrastructure.service;
 
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import org.springframework.stereotype.Component;
 
// ChannelInitializer是默认的initializer,因此需要继承ChannelInitializer类来实现自己的initializer
@Component
public class MyChannelInitializer extends ChannelInitializer<SocketChannel> {
    private static final int MAX_IDLE_SECONDS = 60;
 
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        // 添加到pipeline中的handler会被串行处理(PS: 类似工业生产中的流水线)
        ChannelPipeline pipeline = socketChannel.pipeline();
//        pipeline.addLast("idleStateCheck", new IdleStateHandler(
//                MAX_IDLE_SECONDS, MAX_IDLE_SECONDS, MAX_IDLE_SECONDS));
        // 使用addLast来添加自己定义的handler到pipeline中
        pipeline.addLast("myHandler", new EchoServerHandler());
    }
}