Netty实战(十四)

news/2024/10/21 4:13:47/

WebSocket协议(二)

  • 一、初始化 ChannelPipeline
  • 二、引导
  • 三、加密

一、初始化 ChannelPipeline

我们之前说过为了将 ChannelHandler 安装到 ChannelPipeline 中,需要扩展了ChannelInitializer,并实现 initChannel()方法。

下面我们演示一下:

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;/*** Author: lhd* Data: 2023/6/12* Annotate: 初始化 ChannelPipeline*/
public class ChatServerInitializer extends ChannelInitializer<Channel> {private final ChannelGroup group;public ChatServerInitializer(ChannelGroup group) {this.group = group;}@Overrideprotected void initChannel(Channel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline();//将所有需要的ChannelHandler 添加到 ChannelPipeline 中pipeline.addLast(new HttpServerCodec());pipeline.addLast(new ChunkedWriteHandler());pipeline.addLast(new HttpObjectAggregator(64 * 1024));pipeline.addLast(new HttpRequestHandler("/ws"));pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));pipeline.addLast(new TextWebSocketFrameHandler(group));}
}

我们对于 initChannel()方法的调用,安装所有必需的 ChannelHandler 来设置该新注册的 Channel 的 ChannelPipeline。那么我们安装的这些ChannelHandler 有什么用呢?

ChannelHandler说 明
HttpServerCodec将字节解码为 HttpRequest、HttpContent 和 LastHttpContent。并将 HttpRequest、HttpContent 和 LastHttpContent 编码为字节
ChunkedWriteHandler写入一个文件的内容
HttpObjectAggregator将一个 HttpMessage 和跟随它的多个 HttpContent 聚合为单个 FullHttpRequest 或者 FullHttpResponse(取决于它是被用来处理请求还是响应)。安装了这个之后,ChannelPipeline 中的下一个 ChannelHandler 将只会收到完整的 HTTP 请求或响应
HttpRequestHandler处理 FullHttpRequest(那些不发送到/ws URI 的请求)
WebSocketServerProtocolHandler按照 WebSocket 规范的要求,处理 WebSocket 升级握手、PingWebSocketFrame 、PongWebSocketFrame 和CloseWebSocketFrame
TextWebSocketFrameHandler处理 TextWebSocketFrame 和握手完成事件

Netty 的 WebSocketServerProtocolHandler 处理了所有委托管理的 WebSocket帧类型以及升级握手本身。如果握手成功,那么所需的 ChannelHandler 将会被添加到ChannelPipeline中,而那些不再需要的ChannelHandler 则将会被移除。

WebSocket 协议升级之前的 ChannelPipeline 的状态如下图所示。这代表了刚刚被ChatServerInitializer 初始化之后的 ChannelPipeline。

在这里插入图片描述

WebSocket 协议升级完成之后,WebSocketServerProtocolHandler 将会把 HttpRequestDecoder 替换为 WebSocketFrameDecoder,把 HttpResponseEncoder 替换为WebSocketFrameEncoder。为了性能最大化,它将移除任何不再被 WebSocket 连接所需要的ChannelHandler。其中也包括 HttpObjectAggregator 和 HttpRequestHandler

下图展示了上面操作完成之后的ChannelPipeline,Netty目前支持 4个版本的WebSocket协议,它们每个都具有自己的实现类。Netty将会根据客户端/浏览器所支持的版本 ,自动地选择正确版本WebSocketFrameDecoder和WebSocketFrameEncoder。
在这里插入图片描述

二、引导

我们来写一个引导服务器:

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.util.concurrent.ImmediateEventExecutor;import java.net.InetSocketAddress;/*** Author: lhd* Data: 2023/6/12* Annotate:*/
public class ChatServer {//创建 DefaultChannelGroup,其将保存所有已经连接的WebSocket Channeprivate final ChannelGroup channelGroup = new DefaultChannelGroup(ImmediateEventExecutor.INSTANCE);private final EventLoopGroup group = new NioEventLoopGroup();private Channel channel;//引导服务器public ChannelFuture start(InetSocketAddress address) {ServerBootstrap bootstrap = new ServerBootstrap();bootstrap.group(group).channel(NioServerSocketChannel.class).childHandler(createInitializer(channelGroup));ChannelFuture future = bootstrap.bind(address);future.syncUninterruptibly();channel = future.channel();return future;}//创建 ChatServerInitializeprotected ChannelInitializer<Channel> createInitializer(ChannelGroup group) {return new ChatServerInitializer(group);}//处理服务器关闭,并释放所有的资源public void destroy() {if (channel != null) {channel.close();}channelGroup.close();group.shutdownGracefully();}public static void main(String[] args) throws Exception {if (args.length != 1) {System.err.println("Please give port as argument");System.exit(1);}int port = Integer.parseInt(args[0]);final ChatServer endpoint = new ChatServer();ChannelFuture future = endpoint.start(new InetSocketAddress(port));Runtime.getRuntime().addShutdownHook(new Thread() {@Overridepublic void run() {endpoint.destroy();}});future.channel().closeFuture().syncUninterruptibly();}
}

三、加密

处理好服务器后,下一步就是测试和加密,测试我们之前说过这里不再多说。

这里的加密有两步,一是为 ChannelPipeline 加密,二是为 ChatServer 添加加密。

ChannelPipeline 加密:

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslHandler;/*** Author: lhd* Data: 2023/6/12* Annotate:为 ChannelPipeline 添加加密*/
public class SecureChatServerInitializer extends ChatServerInitializer {private final SslContext context;public SecureChatServerInitializer(ChannelGroup group, SslContext context) {super(group);this.context = context;}@Overrideprotected void initChannel(Channel ch) throws Exception {//调用父类的initChannel()方法super.initChannel(ch);SSLEng.ine engine = context.newEngine(ch.alloc());engine.setUseClientMode(false);// 将SslHandler 添加到ChannelPipeline 中ch.pipeline().addFirst(new SslHandler(engine));}
}

ChatServer 添加加密

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.group.ChannelGroup;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.util.SelfSignedCertificate;import java.net.InetSocketAddress;/*** Author: lhd* Data: 2023/6/12* Annotate:*/
public class SecureChatServer extends ChatServer {//SecureChatServer 扩展 ChatServer 以支持加密private final SslContext context;public SecureChatServer(SslContext context) {this.context = context;}@Overrideprotected ChannelInitializer<Channel> createInitializer(ChannelGroup group) {//返回之前创建的 SecureChatServerInitializer 以启用加密return new SecureChatServerInitializer(group, context);}public static void main(String[] args) throws Exception {if (args.length != 1) {System.err.println("Please give port as argument");System.exit(1);}int port = Integer.parseInt(args[0]);SelfSignedCertificate cert = new SelfSignedCertificate();SslContext context = SslContext.newServerContext(cert.certificate(), cert.privateKey());final SecureChatServer endpoint = new SecureChatServer(context);ChannelFuture future = endpoint.start(new InetSocketAddress(port));Runtime.getRuntime().addShutdownHook(new Thread() {@Overridepublic void run() {endpoint.destroy();}});future.channel().closeFuture().syncUninterruptibly();}
}

http://www.ppmy.cn/news/351252.html

相关文章

解决git提交时候出现的错误提示“modified:xxxxx (modified content, untracked content)“方法

今天来分享一个关于自己在使用git从本地仓库提交至远程仓库时候遇到的一个错误。话不多说&#xff0c;先来看一下这个错误提示&#xff1a;“modified:xxxxx (modified content, untracked content)”。这个错误提示我&#xff0c;xxxxx里面有未跟踪且已修改的内容&#xff0c;…

【C++】optional 用法

返回值可接受为空&#xff0c;用以表示状态失败 举个栗子&#xff0c; #include <iostream> #include <optional>using namespace std;class User{string name;optional<string> nickName;optional<int> age;public:User(const string& name,opti…

大数据分析平台释疑专用帖

大数据分析平台是大数据时代&#xff0c;企业数字化运营决策的一大关键平台&#xff0c;但初次接触大数据分析平台时&#xff0c;大家必然是问题多多的&#xff0c;本文将就几个比较常见的问题进行总结回答&#xff0c;希望对正在了解、选型大数据分析平台的大家有所帮助。 首…

Flash Loader Demonstrator v2.8.0下载

链接&#xff1a;https://pan.baidu.com/s/1jpwH-NU8_Y31hPPDKDuVwA 提取码&#xff1a;2qkb 复制这段内容后打开百度网盘手机App&#xff0c;操作更方便哦

Linux下安装flash player插件

sudo apt install flashplugin-installer sudo apt install browser-plugin-freshplayer-pepperflash中间可能会遇到 sudo apt-get update sudo apt-get install --fix-missing再次运行 sudo apt install browser-plugin-freshplayer-pepperflash直接打开就能看视频了

MTK样机用flashtool烧录操作

这是之前装过了&#xff0c;第一次使用样机&#xff0c;就点击软件右下角的Install安装一下adb驱动 然后就是给电脑安装adb了 不管你用什么方式得到的adb&#xff0c;放到C盘某个目录下&#xff0c;然后我的电脑右击&#xff0c;选择属性&#xff0c;然后点高级系统设置&…

MTK flash tool

1. 从mtk 官网下载SP Flash tool 2. windows版安装运行&#xff0c;Linux运行flash_tool.sh 3. Choose Download agent: 在SP flash tool安装路径里MTK_allInOne_Da.bin 4. Choose scatter-loading file, 在image目录里 5. 选择Download only (或者firmware upgrade) 6. 连…

代替Flash的软件

1、SimpleSVG是国产一款动画制作软件&#xff0c;对矢量图提供一个所见所得动画创作场景&#xff0c;采用传统的绘图和动画调节手段&#xff0c;以智能代码模块&#xff0c;为用户提供高效强大的动画集成创作软件。这款软件虽然是国产动画软件免费使用&#xff0c;但是它有模仿…