hefeixia
2021-02-18 5b8c95c760840f09910730943b21391e47187315
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
package chat.server.netty;  
 
import chat.server.im.IMDispatcher;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;
 
public class NettyHttpChannelInitializer extends ChannelInitializer<SocketChannel> {
 
    
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        
        ChannelHandler decoder = new HttpRequestDecoder();
        ChannelHandler encoder = new HttpResponseEncoder();
        ChannelHandler chunkedWriteHandler = new ChunkedWriteHandler();
        ChannelHandler aggregator = new HttpObjectAggregator(100 * 1024 * 1024);
        ChannelHandler dispatcher = new IMDispatcher();
        
        pipeline.addLast(decoder);
        pipeline.addLast(encoder);
        pipeline.addLast(chunkedWriteHandler);
        pipeline.addLast(aggregator);
        pipeline.addLast(dispatcher);
    }