Pu Zhibing
2025-04-18 9063f00db220dd93c94674d394ddbb38f83abd17
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
package com.sinata.push.util.echo;
 
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
 
import java.nio.charset.Charset;
 
public class ServerInit extends ChannelInitializer<SocketChannel> {
 
    private DiscardServerHandler discardServerHandler = new DiscardServerHandler();
 
    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new StringDecoder(Charset.forName("utf-8")));
        pipeline.addLast(new StringEncoder(Charset.forName("utf-8")));
        // 心跳监测机制
//            pipeline.addLast(new IdleStateHandler(5,7,10, TimeUnit.SECONDS));
        pipeline.addLast(discardServerHandler);
    }
 
}