1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.example.securechat;
17
18 import io.netty5.channel.ChannelInitializer;
19 import io.netty5.channel.ChannelPipeline;
20 import io.netty5.channel.socket.SocketChannel;
21 import io.netty5.handler.codec.DelimiterBasedFrameDecoder;
22 import io.netty5.handler.codec.Delimiters;
23 import io.netty5.handler.codec.string.StringDecoder;
24 import io.netty5.handler.codec.string.StringEncoder;
25 import io.netty5.handler.ssl.SslContext;
26
27
28
29
30 public class SecureChatClientInitializer extends ChannelInitializer<SocketChannel> {
31
32 private final SslContext sslCtx;
33
34 public SecureChatClientInitializer(SslContext sslCtx) {
35 this.sslCtx = sslCtx;
36 }
37
38 @Override
39 public void initChannel(SocketChannel ch) throws Exception {
40 ChannelPipeline pipeline = ch.pipeline();
41
42
43
44
45
46
47 pipeline.addLast(sslCtx.newHandler(ch.bufferAllocator(), SecureChatClient.HOST, SecureChatClient.PORT));
48
49
50 pipeline.addLast(new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
51 pipeline.addLast(new StringDecoder());
52 pipeline.addLast(new StringEncoder());
53
54
55 pipeline.addLast(new SecureChatClientHandler());
56 }
57 }