java netty wss_netty 配置 wss访问

本文档介绍了如何使用Java Netty配置WSS(WebSocket over SSL/TLS)访问。首先,你可以选择自制或获取Let's Encrypt证书来确保安全连接。接着,详细展示了如何在`ChannelHandler`中配置SSL上下文,加载服务器证书和密钥,以及设置HttpServerCodec、HttpObjectAggregator等关键组件,以实现WebSocket服务器的正确运行。
摘要由CSDN通过智能技术生成

# netty 配置 wss访问

## 1.获取证书

可以选择[自制证书](生成自签名证书.md),或者获取 [Let`s Encrypt证书](LetsEncrypt.md)

## 2.配置handler

``` java

public class ChannelHandler extends ChannelInitializer {

@Override

protected void initChannel(SocketChannel channel) throws Exception {

String certPath = "/etc/letsencrypt/live/example.com";

if (new File(certPath).exists()) {

final File certFile = new File(certPath + "/cert.pem");

final File keyFile = new File(certPath + "/privkey.pem");

SslContext sslContext = SslContextBuilder.forServer(certFile, keyFile).build();

channel.pipeline().addLast(sslContext.newHandler(channel.alloc()));

}

// 设置30秒没有读到数据,则触发一个READER_IDLE事件。

// pipeline.addLast(new IdleStateHandler(30, 0, 0));

// HttpServerCodec:将请求和应答消息解码为HTTP消息

channel.pipeline().addLast("http-codec", new HttpServerCodec());

// HttpObjectAggregator:将HTTP消息的多个部分合成一条完整的HTTP消息

channel.pipeline().addLast("aggregator", new HttpObjectAggregator(65536));

// ChunkedWriteHandler:向客户端发送HTML5文件

channel.pipeline().addLast("http-chunked", new ChunkedWriteHandler());

// 在管道中添加我们自己的接收数据实现方法

channel.pipeline().addLast("handler", new MyWebSocketServerHandler());

}

}

```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值