1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package io.netty5.example.http2.helloworld.frame.client;
16
17 import io.netty5.channel.Channel;
18 import io.netty5.channel.ChannelHandlerContext;
19 import io.netty5.channel.ChannelInitializer;
20 import io.netty5.channel.SimpleChannelInboundHandler;
21 import io.netty5.handler.codec.http2.Http2FrameCodec;
22 import io.netty5.handler.codec.http2.Http2FrameCodecBuilder;
23 import io.netty5.handler.codec.http2.Http2MultiplexHandler;
24 import io.netty5.handler.codec.http2.Http2Settings;
25 import io.netty5.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.bufferAllocator()));
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
51 @Override
52 protected void messageReceived(ChannelHandlerContext ctx, Object msg) {
53
54 }
55 }));
56 }
57
58 }