1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty.example.http2.helloworld.frame.client;
16
17 import io.netty.channel.Channel;
18 import io.netty.channel.ChannelHandlerContext;
19 import io.netty.channel.ChannelInitializer;
20 import io.netty.channel.SimpleChannelInboundHandler;
21 import io.netty.handler.codec.http2.Http2FrameCodec;
22 import io.netty.handler.codec.http2.Http2FrameCodecBuilder;
23 import io.netty.handler.codec.http2.Http2MultiplexHandler;
24 import io.netty.handler.codec.http2.Http2Settings;
25 import io.netty.handler.ssl.SslContext;
26
27
28
29
30 public final class Http2ClientFrameInitializer extends ChannelInitializer<Channel> {
31
32 private final SslContext sslCtx;
33
34 public Http2ClientFrameInitializer(SslContext sslCtx) {
35 this.sslCtx = sslCtx;
36 }
37
38 @Override
39 protected void initChannel(Channel ch) throws Exception {
40
41 if (sslCtx != null) {
42 ch.pipeline().addFirst(sslCtx.newHandler(ch.alloc()));
43 }
44
45 final Http2FrameCodec http2FrameCodec = Http2FrameCodecBuilder.forClient()
46 .initialSettings(Http2Settings.defaultSettings())
47 .build();
48 ch.pipeline().addLast(http2FrameCodec);
49 ch.pipeline().addLast(new Http2MultiplexHandler(new SimpleChannelInboundHandler() {
50 @Override
51 protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
52
53 }
54 }));
55 }
56
57 }