iohao / ioGame

无锁异步化、事件驱动架构设计的 java netty 网络编程框架; 轻量级,无需依赖任何第三方中间件或数据库就能支持集群、分布式; 适用于网络游戏服务器、物联网、内部系统及各种需要长连接的场景; 通过 ioGame 你可以很容易的搭建出一个集群无中心节点、集群自动化、分布式的网络服务器;FXGL、Unity、UE、Cocos Creator、Godot、Netty、Protobuf、webSocket、tcp、socket;java Netty 游戏服务器框架;

Home Page:http://game.iohao.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

sslHandler创建

HAZYUHE opened this issue · comments

6IWQBAJ~MBEAJNQ{%D VA9

服务器使用SSL 通过重写pipelineCustom方法,添加SSLHander,在创建sslEngine是,如何获取到SocketChannel,调用.alloc()方法进行创建 sslEngine。还是说通过其他方法创建 sslHander

https://www.yuque.com/iohao/game/ea6geg#u5rfY

netty SSL

public class Example {
    static ExternalServer createExternalServer() {
        int port = 10100;
        DefaultExternalServerBuilder builder = DefaultExternalServer.newBuilder(port);
      
      	// your sslContext
        SslContext sslContext = getSslContext();

        // 对 netty ChannelHandler 编排;重写 MicroBootstrapFlow 的 pipelineCustom 方法
        builder.setting().setMicroBootstrapFlow(new WebSocketMicroBootstrapFlow() {
            @Override
            public void pipelineCustom(PipelineContext context) {
              	// 使用已有的 handler
                super.pipelineCustom(context);

                if (context instanceof DefaultPipelineContext defaultPipelineContext) {
                    Channel channel = defaultPipelineContext.channel();
                    // 添加 SSL/TLS 处理器
                    SslHandler sslHandler = sslContext.newHandler(channel.alloc());
                    context.addFirst(sslHandler);
                }
            }
        });

        return builder.build();
    }

    private static SslContext getSslContext() {
        try {
            // 伪代码 - 创建 SslContext
            SelfSignedCertificate ssc = new SelfSignedCertificate();
            return SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
        } catch (CertificateException | SSLException e) {
            throw new RuntimeException(e);
        }
    }
}