﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-首页技术区</title><link>http://www.blogjava.net/</link><description>专注于Java技术</description><language>zh-cn</language><lastBuildDate>Sun, 17 May 2026 15:21:37 GMT</lastBuildDate><pubDate>Sun, 17 May 2026 15:21:37 GMT</pubDate><ttl>60</ttl><item><title>socket长连接在手游场景下的技术实践</title><link>http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Wed, 06 May 2026 14:27:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451780.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451780.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451780.html</trackback:ping><description><![CDATA[<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文由37手游黄子键分享，有排版和内容优化等。</p><h1>1、引言</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文介绍了37手游基于B站goim框架自研长连接系统的实践。系统采用分层设计，支持多协议和发布/订阅机制，用于直播弹幕、实时推送等场景，实现了高性能与业务适配。</p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="cover_opti" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201852477-1068335934.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201852477-1068335934.jpg" medium-zoom-image="" ls-is-cached=""  lazyloaded"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、长连接对于大部分公司的意义</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">实时的响应总是让人兴奋的，就如你在微信里看到对方正在输入，如你在王者峡谷里一呼百应，如你们在直播弹幕里不约而同的 666，它们的背后都离不开长连接技术的加持。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">每个互联网公司里几乎都有一套长连接系统，它们被应用在消息提醒、即时通讯、推送、直播弹幕、游戏、共享定位、股票行情等等场景。而当公司发展到一定规模，业务场景变得更复杂后，更有可能是多个业务都需要同时使用长连接系统。</p><h1>3、长连接是什么?</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>长连接，在百科上的定义：</strong>指在一个连接上可以连续发送多个数据包，在连接保持期间，如果没有数据包发送，需要双方发链路检测包。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>严格上来说：</strong>长连接是一种概念，它指的是在网络发送，接收双方保持一个持续连接的状态，双方都可以发送或接收消息（全双工）。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">当然，长连接系统听起来好像高深莫测，但实际不可能说完全脱离于我们实际公司的业务去出发设计，这就引出来我们今天的主题&#8212;&#8212; 长连接技术在37手游内是如何设计以及实践的？</p><h1>4、技术痛点</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从服务端而言：</strong>缺乏在SDK内实时推送通知用户的能力，如防沉迷的弹窗通过http定时轮询来实现，给服务造成很大的压力</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从客户端而言：</strong>也缺乏低成本告知服务端自身在线的能力，如维持用户在线状态靠客户端定时上报心跳到防沉迷服务实现。</p><h1>5、搭建背景</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>业务背景：</strong>手游的SDK需要提供内嵌直播弹幕的功能，供玩家在看直播的时候进行沟通发言，业界一般会用到长连接来进行实时的推送，以降低服务的轮训和请求。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从业务上而言：</strong>直播弹幕需求由于初始量级很少，不用通过长连接的形式也能实现具体业务逻辑；但出于平台的拓展和能力考虑，长连接的能力是必须具备的。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">长连接系统的这次搭建，本质是通过SDK内嵌直播弹幕为切入点，从0到1，为平台提供了完整的一套实时推送触达用户的能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从以上角度出发：</strong>我们便着手想要设计一个高可用且高性能的长连接系统提供给我们使用。</p><h1>6、方案选型</h1><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201914846-1664838654.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201914846-1664838654.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从实际上而言，我们考虑三个方向。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><span style="color: #3366ff;"><strong><em>1）</em>云服务：</strong></span></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">市面上可购买的服务如环信等，大多数都以即时聊天通讯为主，长连接往往只是附带产品，过于偏向于社交业务，在花钱的同时也很难适应到自身游戏业务。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><span style="color: #3366ff;"><em>2）</em>开源框架：</span></strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">b站的<a href="https://github.com/Terry-Mao/goim" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">goim</a>框架、<a href="https://github.com/FreddyChen/NettyChat" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">NettyChat</a>框架、<a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a>框架等，好处是免费且能快速接入，但业务还是不相适应，且语言栈和技术栈不一定相契合。</p><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201920865-1888031507.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201920865-1888031507.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><span style="color: #3366ff;"><em>3）</em>自研：</span></strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>弊</em>：</strong>开发成本高，且容易踩坑。<strong><em>利</em>：</strong>基础组件完善，统一框架好进行监控和问题查询。且能充分契合自身业务进行拓展，并将数据源掌控在自己手中。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><span style="color: #3366ff;"><strong><em>4）</em>结论：</strong></span></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">出于扩展性的考虑，肯定是自研的方式更加合适，但完全自研相应的成本和不确定性会非常高，因此最终的选型方案为借鉴b站的goim框架设计和部分代码，并做了符合自身技术栈和业务架构的改善。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">可认为是基于开源框架的自研方案的形式完成了长连接这个系统的落地。</p><h1>7、goim技术概况</h1><h2>7.1 goim是什么？为什么借鉴它？</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">goim是b站开源研发的一个支持集群的im及实时推送服务。业务上为直播间的弹幕发送场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>主要考虑到：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>技术栈契合：语言栈为Golang ，消息队列为kafka，缓存设计为redis；</li><li style="list-style: inherit;"><em>2）</em>高性能：有压测报告，性能设计有保障；</li><li style="list-style: inherit;"><em>3）</em>多协议支持：websocket、tcp。</li></ul><h2>7.2 goim的模块？</h2><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201927412-901970329.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201927412-901970329.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>如上图所示：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>comet：用于跟端上保连，在内存中存储订阅信息；</li><li style="list-style: inherit;"><em>2）</em>logic：用于处理鉴权，在线房间等业务逻辑；</li><li style="list-style: inherit;"><em>3）</em>business：业务服务；</li><li style="list-style: inherit;"><em>4）</em>balancer：负载均衡模块；</li><li style="list-style: inherit;"><em>5）</em>discovery：服务发现模块；</li><li style="list-style: inherit;"><em>6）</em>job：kafka用于削峰。redis用于缓存房间信息；</li><li style="list-style: inherit;"><em>7）</em>logic和comet的通讯：采用rpc，优化性能。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从上面的goim的设计而言，我们可以总结出长连接的设计原则。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>长连接架构的设计总是大同小异的，通用的有这几点：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>业务层次分明：分接入层，逻辑层，存储层，服务发现层；通过这几层，实现业务解耦</li><li style="list-style: inherit;"><em>2）</em>协议通用：传输的数据协议必须在所有服务内进行通讯，并支持扩展</li><li style="list-style: inherit;"><em>3）</em>上行和下行收敛：上行和下行都会有一个对应的网关来进行收敛消息的收敛</li><li style="list-style: inherit;"><em>4）</em>消息削峰：通过队列来将发送信息削峰。并使接入层和逻辑层解藕</li></ul><h1>8、手游长连接系统的设计实践</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">根据以上的通用设计原则，我们不难得出，需要针对goim的架构如何做适配才能实际满足我们的业务需求，下面我们将会一一介绍。</p><h2>8.1 架构改动</h2><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201936024-1704414491.png" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201936024-1704414491.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;"><strong>从架构而言，我们改动的点不多：</strong></p></div></div></div><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>cient获取连接节点：手游内无统一的服务发现模块，但有外层LB，因此通过LB来实现comet节点负载均衡</li><li style="list-style: inherit;"><em>2）</em>推送消息：同上，消费者获取节点时需要到logic服务中查询，而不是discovery</li><li style="list-style: inherit;"><em>3）</em>服务通讯协议：由于手游内架构通讯协议的统一，接入层和逻辑层的通讯由RPC转为http</li></ul><h2>8.2 逻辑改动</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em>鉴权机制：</strong>客户端第一次连接上comet的时候，会发送鉴权上行。comet会解析客户端传输的数据，若包体协议或对应的签名错误，comet会将会直接主动断连</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em>心跳机制：</strong>comet会有心跳计时器，若客户端无定时上报心跳，则认为该连接已经超时，直接断开。这种业务的心跳主要是为了防止僵尸连接的存在。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>3）</em>回包机制：</strong>客户端每发送一次上行操作，comet都会有对应的消息回包给端上。端上可根据回包，来知道自己鉴权，心跳，或者订阅，退订的操作是否成功，从而决定是否进行重试。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>4）</em>发布/订阅机制：</strong></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们在观看goim的源码时，发现goim适用于直播，群体推送以房间为维度，带有强业务属性，因此我们针对该部分抽象出发布/订阅机制。将房间抽象为topic，修改进房/出房动作为&nbsp;&nbsp;订阅/退订。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">将所有的通知抽象为业务事件，客户端想要接受到哪个事件过来的消息时，可发送对应的订阅上行。单个连接订阅的事件不做限制，对某个用户或某个事件范围内的用户推送消息时，comet会根据事件去取到推送的用户，只有用户订阅了才会收到消息。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>5）</em>内存设计：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;">a. bucket维护消息通道和事件的信息；</li><li style="list-style: inherit;">b. 一个session对应一个用户连接；</li><li style="list-style: inherit;">c. 根据sessionid做一致性哈希来选择落到那个bucket上；</li><li style="list-style: inherit;">d. bucket有两个map，一个是session map，一个是topic map；</li><li style="list-style: inherit;">e. 所有bucket都会开启一个chan做监听，广播的时候，会通知到所有bucket，所有bucket再取出某个事件的所有连接进行下发。</li></ul><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201943167-1033383934.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201943167-1033383934.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>8.3 总体交互</h2><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201950322-1618265421.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506201950322-1618265421.jpg" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>8.4 总体特点</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>因此，我们可以总结出来，我们手游长连接系统的总体特点：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>纯golang实现；</li><li style="list-style: inherit;"><em>2）</em>多协议支持：websocket和tcp；</li><li style="list-style: inherit;"><em>3）</em>可拓扑结构：主要模块均无状态，可横向扩展；</li><li style="list-style: inherit;"><em>4）</em>消息支持单推/群推，消息协议业务可自定义；</li><li style="list-style: inherit;"><em>5）</em>发布/订阅机制，事件可业务自定义。</li></ul><h1>9、性能评估</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在说性能之前，我们先抛出一个疑问：协程数过多实际占用的是什么？连接数过多是否影响CPU？</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>从实际上来说：</strong>长连接的性能瓶颈一般卡在接入层，因此以接入层为评估维度，通过全链路压测得出来结果。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>如下：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>压测参数：3000连接+ 3000 qps的实时推送；</li><li style="list-style: inherit;"><em>2）</em>推送内容：{msg:test}&nbsp;&nbsp;；</li><li style="list-style: inherit;"><em>3）</em>推送类型：群推；</li><li style="list-style: inherit;"><em>4）</em>推送持续时长：5分钟 ；</li><li style="list-style: inherit;"><em>5）</em>资源使用：1核2G的容器；</li><li style="list-style: inherit;"><em>6）</em>CPU的使用：达到100%。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前采用的长连接系统，通过压测结果发现，本身的连接数的维持和实时推送实际影响到是机器两个维度的性能，推送量影响到的是CPU，连接数影响到的是内存。分别提高两个参数对相应的内存，CPU的影响并不大。</p><h1>10、实际应用情况</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>直播弹幕：</strong></p><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202001563-268800190.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202001563-268800190.jpg"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>悬浮球实时红点：</strong></p><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202006981-45886119.jpg" src="https://img2024.cnblogs.com/blog/1834368/202605/1834368-20260506202006981-45886119.jpg"  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">从实际上来说，接入的业务虽小，但基础能力已经具备。</p></div></div></div><h1>11、本文小结</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">长连接系统从根本上来说，对应的设计都是大同小异的，我们应该更加关注的是对于自身业务的适配以及实现。</p><h1>12、参考资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/thread-1915-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">以网游服务端的网络接入层设计为例，理解实时通信的技术挑战</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/thread-2737-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">知乎千万级并发的高性能长连接网关技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-3110-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">手淘亿级移动端接入层网关的技术演进之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-3564-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">喜马拉雅自研亿级API网关技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-3757-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">石墨文档单机50万WebSocket长连接架构实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-3860-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">小米小爱单机120万长连接接入层的架构演进</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-3941-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">B站基于微服务的API网关从0到1的演进之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-4618-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">去哪儿网酒店高性能业务网关技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-4623-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百度基于Go的千万级统一长连接服务架构实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-4641-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">揭秘腾讯公网TGW网关系统的技术架构演进</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-4854-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">基于Netty的携程高性能网关异步改造实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-4623-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">手把手教你写基于TCP的Socket长连接</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-2799-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">正确理解IM长连接、心跳及重连机制，并动手实现</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-3908-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">万字长文：手把手教你实现一套高效的IM长连接自适应心跳保活机制</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15]&nbsp;<a href="http://www.52im.net/thread-2106-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1;">用JWT技术解决IM系统Socket长连接的身份认证痛点</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16]&nbsp;<a href="http://www.52im.net/topic-tcpipvol1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">TCP/IP详解</a>&nbsp;-&nbsp;<a href="http://docs.52im.net/extend/docs/book/tcpip/vol1/11/" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">第11章&#183;UDP：用户数据报协议</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17]&nbsp;<a href="http://www.52im.net/topic-tcpipvol1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">TCP/IP详解</a>&nbsp;-&nbsp;<a href="http://docs.52im.net/extend/docs/book/tcpip/vol1/17/" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">第17章&#183;TCP：传输控制协议</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[18]&nbsp;<a href="http://www.52im.net/thread-3134-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">WebSocket从入门到精通，半小时就够！</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[19]&nbsp;<a href="http://www.52im.net/thread-1107-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">快速理解TCP协议一篇就够</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[20]&nbsp;<a href="http://www.52im.net/thread-1160-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">快速理解TCP和UDP的差异</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[21]&nbsp;<a href="http://www.52im.net/thread-3793-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一泡尿的时间，快速搞懂TCP和UDP的区别</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[22]&nbsp;<a href="http://www.52im.net/thread-3821-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">到底什么是Socket？一文即懂！</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[23]&nbsp;<a href="http://www.52im.net/thread-1732-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">我们在读写Socket时，究竟在读写什么？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[24]&nbsp;<a href="http://www.52im.net/thread-3339-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">假如你来设计TCP协议，会怎么做？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[25]&nbsp;<a href="http://www.52im.net/thread-4146-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">深入操作系统，一文搞懂Socket到底是什么</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文同步发布于：</strong><a href="http://www.52im.net/thread-4907-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4907-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451780.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-05-06 22:27 <a href="http://www.blogjava.net/jb2011/archive/2026/05/06/451780.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>基于开源MobileIMSDK框架，即时通讯IM产品RainbowChat v12.0已发布</title><link>http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Mon, 27 Apr 2026 09:22:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451779.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451779.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451779.html</trackback:ping><description><![CDATA[<div id="cnblogs_post_body"  blogpost-body-html"="" style="margin-bottom: 20px; word-break: break-word; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><h1>1、关于RainbowChat</h1><p style="margin: 10px auto;">&nbsp;<img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210602156-1614496586.jpg" alt="1" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><p style="margin: 10px auto;">RainbowChat是一套基于开源IM即时通讯聊天框架&nbsp;<a href="http://www.52im.net/thread-52-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a>&nbsp;的<strong>产品级移动端IM系统</strong>。<strong>RainbowChat源于真实运营的产</strong>品，解决了大量的屏幕适配、细节优化、机器兼容问题。<strong>RainbowChat可能是市面上提供im即时通讯聊天源码的，唯一一款同时支持TCP、UDP、WebSocket三种通信协议的IM产品</strong>。与姊妹产品&nbsp;<a href="http://www.52im.net/thread-4824-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">RainbowTalk</a>&nbsp;和&nbsp;<a href="http://www.52im.net/thread-2483-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">RainbowChat-Web</a>&nbsp;技术同源，历经考验。</p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;详细介绍：</strong><a href="http://www.52im.net/thread-19-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-19-1-1.html</a></li><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;版本日志：</strong><a href="http://www.52im.net/thread-2735-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-2735-1-1.html</a></li><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;运行截图：</strong><a href="http://www.52im.net/thread-20-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">Android端</a>、<a href="http://www.52im.net/thread-2730-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">iOS端</a></li><li style="list-style: inherit;"><strong><strong>&#9758;</strong>&nbsp;下载体验：</strong><a href="https://apps.apple.com/cn/app/rainbowchat/id1423024212" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">App Store安装地址</a>&nbsp;（另：Android端下载体验&nbsp;<a href="http://www.52im.net/thread-4739-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">点此查看</a>）</li></ul><h1>2、关于MobileIMSDK开源工程</h1><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210610330-1388464167.jpg" alt="2" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><div align="left"><p style="margin: 10px auto;"><strong>MobileIMSDK</strong>&nbsp;是一套全平台开源IM即时通讯聊天框架，超轻量级、高度提炼，一套API优雅支持<strong>UDP</strong>&nbsp;、<strong>TCP</strong>&nbsp;、<strong>WebSocket</strong>&nbsp;三种协议，客户端支持<strong>iOS、Android、H5、小程序、Uniapp、标准Java、纯血鸿蒙</strong>等，服务端基于Netty编写，性能卓越、易于扩展。</p><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210620811-1338174721.png" alt="3" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></div><p data-pid="9Wt-ivaI" style="margin: 10px auto;"><strong>工程同步开源地址：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;">❶&nbsp;<strong>GitHub：</strong><a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a></li><li style="list-style: inherit;">❷&nbsp;<strong>码云gitee：</strong>&nbsp;<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://gitee.com/jackjiang/MobileIMSDK</a></li><li style="list-style: inherit;">❸&nbsp;<strong>Gitcode：</strong><a href="https://gitcode.com/hellojackjiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://gitcode.com/hellojackjiang2011/MobileIMSDK</a></li></ul><h1>2、v12.0 版更新内容</h1><p style="margin: 10px auto;"><strong>此版更新内容</strong>（<a href="http://www.52im.net/thread-1217-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">更多历史更新日志</a>）：<br /><br /><strong>（1）Android端主要更新内容</strong>【<span style="color: #888888;">全面适配Android 16、适配16KB page size、适配全面屏特性等</span>】：</p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;"><em>1）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了两个表情占位符重复的问题；</li><li style="list-style: inherit;"><em>2）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了好友列表删除唯一好友后，一直转圈的问题；</li><li style="list-style: inherit;"><em>3）</em>[<span style="color: #ff4d4e;">bug</span>] 优化了搜索聊天记录时，当首页&#8220;消息&#8221;中不存在该陌生人时，搜出的群聊详细中消息发送者昵称会用uid显示的问题；</li><li style="list-style: inherit;"><em>4）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了不支持分区存储的老手机转发的大文件消息，在新系统上无法下载的问题；</li><li style="list-style: inherit;"><em>5）</em>[<span style="color: #ff4d4e;">bug</span>] 优化了存在多条置顶消息时，不是按置顶时间而是消息时间排序的问题；</li><li style="list-style: inherit;"><em>6）</em>[<span style="color: #00b050;">新增</span>] 二维码生成界面下方增加功能按钮；</li><li style="list-style: inherit;"><em>7）</em>[<span style="color: #00b050;">新增</span>] &#8220;用户信息&#8221;界面增加了&#8220;查看用户资料&#8221;按钮；</li><li style="list-style: inherit;"><em>8）</em>[<span style="color: #00b050;">新增</span>] 优化了世界频道的打开入口等；</li><li style="list-style: inherit;"><em>9）</em>[<span style="color: #00b050;">新增</span>] 去掉了&#8220;商城&#8221;模块，增加了&#8220;发现&#8221;页面；</li><li style="list-style: inherit;"><em>10）</em>[<span style="color: #909296;">优化</span>] 将核心层提炼成独立的chatkit模块；</li><li style="list-style: inherit;"><em>11）</em>[<span style="color: #909296;">优化</span>] 解决了独立chatkit后，好友信息中删除对方时，无法自动跳转到主页的问题；</li><li style="list-style: inherit;"><em>12）</em>[<span style="color: #909296;">优化</span>] 现在不能删除首页列表中的&#8220;确认提醒&#8221;这个item了；</li><li style="list-style: inherit;"><em>13）</em>[<span style="color: #909296;">优化</span>] 升级腾讯Bugly至4.1.9.3，解决上架国内应用市场的隐私合规问题；</li><li style="list-style: inherit;"><em>14）</em>[<span style="color: #909296;">优化</span>] 登录和退出登录接口中废弃了osType字段；</li><li style="list-style: inherit;"><em>15）</em>[<span style="color: #909296;">优化</span>] 优化了注册界面中关于服务端返回邮箱格式不正确的错误码的处理；</li><li style="list-style: inherit;"><em>16）</em>[<span style="color: #909296;">优化</span>] 支持小窗、分屏显示；</li><li style="list-style: inherit;"><em>17）</em>[<span style="color: #909296;">优化</span>] 只有好友才能查看对方的注册和登录时间；</li><li style="list-style: inherit;"><em>18）</em>[<span style="color: #909296;">优化</span>] 查找好友时不再显示对方的在线状态；</li><li style="list-style: inherit;"><em>19）</em>[<span style="color: #909296;">优化</span>]&nbsp;<span style="color: #ff8c00;">提升targetSdkVersion至36，全面兼容Android 16</span>；</li><li style="list-style: inherit;"><em>20）</em>[<span style="color: #909296;">优化</span>]&nbsp;<span style="color: #ff8c00;">开发工程升级适配AGP 9.1最新版</span>；</li><li style="list-style: inherit;"><em>21）</em>[<span style="color: #909296;">优化</span>] 升级权限框架以适配最新Android 16系统；</li><li style="list-style: inherit;"><em>22）</em>[<span style="color: #909296;">优化</span>] 针对<span style="color: #ff8c00;">全部界面适配系统强制的Edge to Edge全面屏特性</span>；</li><li style="list-style: inherit;"><em>23）</em>[<span style="color: #909296;">优化</span>] 解决了Android 16下聊天界面输入法弹出时会挡住消息输入框的问题；</li><li style="list-style: inherit;"><em>24）</em>[<span style="color: #909296;">优化</span>] 解决基于PopupWindow实现的弹出界面底部在Edge to Edge全面屏特性下的显示问题；</li><li style="list-style: inherit;"><em>25）</em>[<span style="color: #909296;">优化</span>] 加固一处因多线程安全问题导致的可能崩溃风险；</li><li style="list-style: inherit;"><em>26）</em>[<span style="color: #909296;">优化</span>] 升级高德地图SDK至最新v11.1等，<span style="color: #ff8c00;">适配google play强制16KB page size问题</span>；</li><li style="list-style: inherit;"><em>27）</em>[<span style="color: #909296;">优化</span>] 优化了位置消息搜索界面的搜索组件ui并提升了细节体验；</li><li style="list-style: inherit;"><em>28）</em>[<span style="color: #909296;">优化</span>] 解决了进入了主页搜索界面在Android 16下不能自动弹出输入法，及优化了点击背景可收起软键盘；</li><li style="list-style: inherit;"><em>29）</em>[<span style="color: #909296;">优化</span>] 删减了APP首次启动时的权限申请内容；</li><li style="list-style: inherit;"><em>30）</em>[<span style="color: #909296;">优化</span>] 解决了Android 16下返回按钮事件捕获失败的问题；</li><li style="list-style: inherit;"><em>31）</em>[<span style="color: #909296;">优化</span>] 聊天界面下方的功能面板图标美化等；</li><li style="list-style: inherit;"><em>32）</em>[<span style="color: #909296;">优化</span>] 聊天文本框自动换行；</li><li style="list-style: inherit;"><em>33）</em>[<span style="color: #909296;">优化</span>] 其它更具现代感的UI细节优化和体验等；</li></ul><p style="margin: 10px auto;"><strong>（2）服务端主要更新内容</strong>【<span style="color: #888888;">安全加固、新增接口等</span>】：</p><ul style="margin-left: 2.5rem; padding-left: 0px;"><li style="list-style: inherit;"><em>1）</em>[<span style="color: #ff4d4e;">bug</span>] 解决了对接RainbowChat-Web产品时，网页端无法正常登录的问题；</li><li style="list-style: inherit;"><em>2）</em>[<span style="color: #909296;">优化</span>] 加固了后端SQL防注入逻辑；</li><li style="list-style: inherit;"><em>3）</em>[<span style="color: #909296;">优化</span>] 开启了WebSocket协议支持；</li><li style="list-style: inherit;"><em>4）</em>[<span style="color: #909296;">优化</span>] 对离线数据表中的消息指纹字段增加了索引，提升查询性能；</li><li style="list-style: inherit;"><em>5）</em>[<span style="color: #909296;">优化</span>] 优化了文件下载服务中存在利用文件名进行越权文件操作的安全隐患；</li><li style="list-style: inherit;"><em>6）</em>[<span style="color: #909296;">优化</span>] 提供了一个校验token与uid一致性的安全性实现示例；</li><li style="list-style: inherit;"><em>7）</em>[<span style="color: #909296;">优化</span>] 优化了原Android专用的登录接口【接口1009】，使之同时支持验证码、密码登录；</li><li style="list-style: inherit;"><em>8）</em>[<span style="color: #909296;">优化</span>] 【接口1008-10-22】新增了&#8220;preview_count&#8221;字段；</li><li style="list-style: inherit;"><em>9）</em>[<span style="color: #909296;">优化</span>] 将IDEA工程中applicationContextRoot改成了rainbowchat_pro/（方便开发环境跟生产环境一致）;</li><li style="list-style: inherit;"><em>10）</em>[<span style="color: #909296;">优化</span>] 优化了注册接口【接口1008-1-7】，增加了手机号和短信验证码支持；</li><li style="list-style: inherit;"><em>11）</em>[<span style="color: #00b050;">新增</span>] 数据库新增了注销登录相关字段；</li><li style="list-style: inherit;"><em>12）</em>[<span style="color: #00b050;">新增</span>] 新增注销登录接口；</li><li style="list-style: inherit;"><em>13）</em>[<span style="color: #00b050;">新增</span>] 新增获取验证码接口【接口1008-1-27】；</li><li style="list-style: inherit;"><em>14）</em>[<span style="color: #00b050;">新增</span>] 新增新的登录接口【接口1017】，同时支持ios等客户端的验证码、密码登录；</li><li style="list-style: inherit;"><em>15）</em>[<span style="color: #00b050;">新增</span>] 新增对接鸿蒙NEXT产品时支持华为Push Kit离线推送；</li></ul><h2>3、升级后的主要UI运行截图</h2><p style="margin: 10px auto;">（<strong><strong>&#9758;</strong>&nbsp;</strong>更多截图请查看：<a href="http://www.52im.net/thread-20-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">Android端运行截图</a>、<a href="http://www.52im.net/thread-2730-1-1.html" target="_blank" style="color: #1d58d1; text-decoration: none;">iOS端运行截图</a>）</p><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210722057-491636925.jpg" alt="4_s100_p75" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p><h2>4、真机运行视频</h2><p style="margin: 10px auto;">（&#9758;&nbsp;<a href="https://www.bilibili.com/video/BV1ZHoMBbEvC/" target="_blank" style="color: #1d58d1; text-decoration: none;">新窗口中查看真机运行视频</a>）</p><p style="margin: 10px auto;"><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260428145800484-1457756756.gif" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260428145800484-1457756756.gif"  medium-zoom-image"="" style="border: 0px; max-width: 100%; cursor: zoom-in; height: auto !important; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><br /></p><h2>5、真机实拍截图</h2><p style="margin: 10px auto;"><img src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260426210751290-767045076.jpg" alt="6_pct85" loading="lazy" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></p></div><div id="MySignature" role="contentinfo" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div id="AllanboltSignature"></div></div><img src ="http://www.blogjava.net/jb2011/aggbug/451779.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-04-27 17:22 <a href="http://www.blogjava.net/jb2011/archive/2026/04/27/451779.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>社交场景下的统一即时通讯im消息流交互层模块化技术实践</title><link>http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html</link><dc:creator>Jack Jiang</dc:creator><author>Jack Jiang</author><pubDate>Mon, 20 Apr 2026 10:43:00 GMT</pubDate><guid>http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html</guid><wfw:comment>http://www.blogjava.net/jb2011/comments/451777.html</wfw:comment><comments>http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/jb2011/comments/commentRss/451777.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/jb2011/services/trackbacks/451777.html</trackback:ping><description><![CDATA[<p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">网易技术团队旭风分享，有排版优化和修订。</p><h1>1、引言</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一款社交产品的诞生，离不开即时通讯（IM）场景。随着团队业务版图在社交领域的布局，诞生了多个社交场景APP，涉及的IM场景，包含私聊、群聊、聊天室等。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>这些IM场景，在消息流的展示形式上是极为相似的，同时每个业务又有着自己特殊的交互需求。基于此，我们对IM消息流能力做了标准化的构建，来减少IM功能的业务接入成本；同时也是为了统一各个业务的技术方案，减少跨业务开发的理解和维护成本。</strong>本文主要针对iOS端在IM消息流交互层的设计上，提供一些实践思路。</p><div img-uploading-status=""  image-package-1"="" data-index="1" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="cover_opti" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172238382-1940825930.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172238382-1940825930.png" medium-zoom-image="" ls-is-cached=""  lazyloaded"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>2、业界的实现方案</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前业界有各种即时通讯服务商提供的配套交互层解决方案，其大多以牺牲灵活性来满足快速集成需要，在定制能力上远不能胜任我们业务需要。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">再诸如 MessageKit之类的社区IM框架，其在视觉交互表现上功能完备，能帮助我们快速、灵活搭建IM消息流结构，但业务需要的是一套完整的携带消息交互能力的方案，因此对此类框架，仍需要做不小的改造才能适应我们的业务（另一参考方案：<a href="https://gitee.com/jackjiang/MobileIMSDK" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">MobileIMSDK</a>（<a href="https://www.cnblogs.com/imteck4713/p/gitee.com/jackjiang/MobileIMSDK" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">Gitee源码托管地址</a>））。</p><h1>3、我们的想法</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>对于一个IM消息流交互层方案，主要考虑几个方面：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>规范的消息流结构：提供消息流视图结构规范化的构建方式；</li><li style="list-style: inherit;"><em>2）</em>标准的消息交互能力：统一消息交互能力，业务方按需使用，快速集成；</li><li style="list-style: inherit;"><em>3）</em>业务拓展性：针对数据源、消息交互能力提供业务灵活拓展点；</li><li style="list-style: inherit;"><em>4）</em>业务接入成本：内置通用交互方案，降低业务接入成本。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">目前，我们存量业务中的IM场景，底层IM能力主要由云信引擎提供。同时又存在基于业务服务端，通过HTTP去交互的场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">另外，还需要预留后期切换IM引擎的可能性，因此需要将交互层IM能力抽象出来。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">此外，为了适应团队现状，减小业务接入成本，考虑将云信提供的交互能力内置在方案中。</p><h1>4、整体设计</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>设计愿景：</strong>提供标准化的能力，同时对拓展开放。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们期望一套通用的IM消息流能力，能够在方案上标准化。这里的标准化，主要包含消息流结构构建的标准化，以及消息交互能力的标准化。同时，方案需要在交互能力上适应不同业务场景，因此采用依赖注入的方式，提供业务定制能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>按照职能划分，将框架整体分为了两层：</strong></p><div img-uploading-status=""  image-package-2"="" data-index="2" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="1" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172248072-572198573.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172248072-572198573.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /><p style="margin: 10px auto;">&nbsp;</p></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>1）</em></strong>消息流结构层：负责消息流结构的构建，定义消息视图、布局、数据上的规范，提供业务层分别在「消息」、「会话」两个维度的配置能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong><em>2）</em></strong>消息交互层：提供消息能力、消息流、消息数据方面的交互能力，向下依赖交互接口，内置标准交互能力的同时，也支持业务按需注入交互实现。</p><h1>5、聊天消息流的显示结构</h1><h2>5.1 消息组件</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">不同的业务场景，消息流样式表现必然有所差异。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>下面列出了我们几个业务中的消息流界面：</strong></p><div img-uploading-status=""  image-package-3"="" data-index="3" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="2" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172253118-172076407.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172253118-172076407.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">如何设计一套通用的消息流视图结构，满足不同业务需要？经过对各个业务以及一些主流IM工具的观察，将消息视图结构设计成如下结构，是能够满足我们各个IM场景需要的（见下图）。</p><div img-uploading-status=""  image-package-4"="" data-index="4" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="3" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172300245-2086082730.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172300245-2086082730.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我将消息结构拆分成了5部分，对应5个消息组件&nbsp;&nbsp;MessageView ，每个消息组件都支持业务对其「样式」、「显隐」、「布局」进行配置，从而满足不同场景定制需要。</p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">MessageView作为基础消息组件，提供了一些标准能力，例如是否响应菜单动作 canPerformMenuAction 、视图重用回调时机 prepareForReuse 、尺寸策略等。</p><p style="margin: 10px auto;">open&nbsp;class&nbsp;MessageView: MessageAbstractView {</p><p style="margin: 10px auto;">&nbsp;&nbsp;public&nbsp;var&nbsp;canPerformMenuAction =&nbsp;false</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;open func refresh(with&nbsp;message: Message) {}</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;open func prepareForReuse() {}</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;open&nbsp;class&nbsp;func createSizeStrategy(message: Message, fittingSize: CGSize) -&gt; MessageLayoutSizeStrategy? {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">}</p></blockquote><h2>5.2 尺寸策略</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">消息组件尺寸作为消息流布局上不可或缺的要素，方案提供了多种尺寸计算策略 MessageLayoutSizeStrategy 。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>具体是：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>自动布局计算策略：业务方对消息组件使用 AutoLayout 布局时使用，内部会依据约束自动计算好组件尺寸；</li><li style="list-style: inherit;"><em>2）</em>SizeThatFit 策略：依据组件 SizeThatFit 方法返回的尺寸进行布局；</li><li style="list-style: inherit;"><em>3）</em>自定义策略：提供自定义尺寸计算方式。</li></ul><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol MessageLayoutSizeStrategy {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func caclulateSize(_ sizeViewType: MessageView.Type,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: Message,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fittingSize: CGSize) -&gt; CGSize</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">public struct MessageAutoLayoutSizeStrategy: MessageLayoutSizeStrategy {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;public func caclulateSize(_ sizeViewType: MessageView.Type,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: Message,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fittingSize: CGSize) -&gt; CGSize {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sizeView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">public struct MessageSizeThatFitsStrategy: MessageLayoutSizeStrategy {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;public func caclulateSize(_ sizeViewType: MessageView.Type,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;message: Message,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fittingSize: CGSize) -&gt; CGSize&nbsp; {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;sizeView.sizeThatFits(fittingSize)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;}</p><p style="margin: 10px auto;">}</p></blockquote><h2>5.3 布局快照</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们还针对消息组件维度支持了布局快照。通常当一个消息组件尺寸固定，在交互过程中尺寸不会发生的情况下，打开布局快照，以减少布局计算消耗。同时也提供了快照清除的能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">我们对多个消息流在快速滚动过程中的CPU峰值做了统计，在使用自动布局尺寸策略的情况下，开启布局快照，峰值降低了10%~20%。</p><div img-uploading-status=""  image-package-5"="" data-index="5" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="4" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172308798-1399375821.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172308798-1399375821.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>5.4 交互事件</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">另外在手势交互上，对外暴露了各个消息组件的一系列交互事件。常见的场景例如单击浏览消息内容，长按展示消息菜单等。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">方案内部提供了基于系统样式的长按菜单，并提供上层菜单配置能力，同时也可以基于暴露的长按手势事件来自定义菜单。</p><h2>5.5 消息流</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">一个会话对应一个流，方案也提供了消息流在会话维度上的一些标准化配置。例如消息分页数量、是否自动拉取历史消息、是否开启增量刷新，以及在时间展示上的样式配置等。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">此外为了减少列表重绘，消息流也支持增量刷新。通常情况下业务层不需要主动刷新列表，只需对消息数据进行增删改操作，内部会触发对数据源的「diff-update」计算，从而驱动列表的增量更新。</p><div img-uploading-status=""  image-package-6"="" data-index="6" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="5" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172314511-1003407726.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172314511-1003407726.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h1>6、聊天消息交互层</h1><h2>6.1 概述</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>对于业务方而言，在消息交互上通常关心这么几点：</strong></p><ul style="margin-left: 2.5rem; padding-left: 0px; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><li style="list-style: inherit;"><em>1）</em>提供了哪些标准化的交互能力；</li><li style="list-style: inherit;"><em>2）</em>如何拓展自定义的交互实现；</li><li style="list-style: inherit;"><em>3）</em>如何对交互流程进行干预。</li></ul><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">结合团队现状，我们在方案内部内置了基于某信的IM交互能力，同时定义了相关交互接口，供业务方按需注入实现。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在实际业务中，一个APP内可能存在多个IM场景，因此交互能力支持按会话维度进行注入，各个会话之间的交互是相互隔离的。</p><h2>6.2 消息源</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">不同的IM场景，消息数据来源可能存在差异。例如我们私聊、群聊的数据源来自云信数据同步服务，聊天室数据需要通过云信提供的历史消息接口拉取，另外也存在诸如通过业务服务端接口来拉取消息数据的场景。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">因此方案上设置了数据源接口 SessionMessageProvider ，提供不同场景消息源的定制能力。</p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol SessionMessageProvider {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func messages(in&nbsp;session: Session,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;anchorMessage: Message?,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;limit: Int,</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;completion: @escaping ([Message]) -&gt; Void)</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">方案设置了一个负责管理消息数据源的 DataManager 实例， 其依赖 SessionMessageProvider 提供的数据源。同时内置了基于云信的数据源获取实现，能够根据当前会话类型，获取私聊、群聊、聊天室的数据源。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">如果当前场景是通过HTTP拉取消息的，则需要业务上层手动注入一个从接口获取数据源的 SessionMessageProvider 实例。</p><div img-uploading-status=""  image-package-7"="" data-index="7" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="6" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172322020-1289552147.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172322020-1289552147.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>6.3 交互源</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">方案提供了IM标准交互能力，例如消息收发、消息撤回、保存等，以统一各业务交互姿势。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">具体的交互源除了要考虑目前包含的云信及业务服务端，也要适应其他交互源，因此将交互实现部分也抽象出了接口 MessageServiceInterface 。业务根据当前实际场景，注入具体的交互实现即可。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>下面列出了一些交互申明：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol MessageServiceInterface {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func send(message: Message,&nbsp;in&nbsp;session: Session, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func resend(message: Message, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func forward(message: Message, to session: Session, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func revoke(message: Message, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func save(message: Message,&nbsp;in&nbsp;session: Session, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func&nbsp;delete(message: Message, completion: @escaping MessageServiceInterfaceCompletion)</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">同样，我们也内置了一些通用交互方案，例如支持云信提供的私聊群聊交互能力，以及由中台提供的通用聊天室服务交互能力，以支持相关场景下快速接入。</p><div img-uploading-status=""  image-package-8"="" data-index="8" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="7" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172331316-289718956.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172331316-289718956.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><h2>6.4 交互钩子</h2><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在实际IM业务开发过程中，往往需要对交互流程做一些干预，或是在交互过程中做一些定制化的动作。因此方案也提供了一些交互钩子，支持「交互前置校验」、「交互前准备」。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>以消息发送流程为例，提供了「发送前校验」、「发送准备」两个消息发送过程的回调钩子：</strong></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;">public protocol MessageServicePrechecker {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;// 消息发送前置校验&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func shouldSend(message: Message,&nbsp;in&nbsp;session: Session) -&gt; Bool</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">}</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">public protocol MessageServicePreparation {</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;/// 准备发送准备</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;func prepareSend(message: Message,&nbsp;in&nbsp;session: Session, callback: @escaping MessageServicePreparationCallback)</p><p style="margin: 10px auto;">&nbsp;</p><p style="margin: 10px auto;">&nbsp;&nbsp;&nbsp;&nbsp;// ...省略其他代码</p><p style="margin: 10px auto;">}</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>整体的发送流程如图所示：</strong></p><div img-uploading-status=""  image-package-9"="" data-index="9" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="8" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172339025-249421311.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172339025-249421311.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">前置校验阶段，用来作消息发送前的校验工作，根据实际状态决定消息是否可以发送。发送准备阶段，则可以在消息投递前做最后的准备工作，例如海外业务可以在这里处理消息资源附件上传Amazon，或是在此处对消息塞入一些客户端信息、反作弊Token等，支持异步操作。</p><h1>7、业务接入能力</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">业务只需要在上层提供针对消息以及会话两个维度的配置，就能基于内置的交互能力，构建出一套基础的IM消息流能力。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在具体的消息样式呈现上，则通常需要业务层维护一组关于「消息类型-消息组件类型-消息结构」的映射关系。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><strong>具体关联如下：</strong></p><div img-uploading-status=""  image-package-10"="" data-index="10" style="font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;"><div><div><img alt="9" loading="lazy" data-src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172344348-320868519.png" src="https://img2024.cnblogs.com/blog/1834368/202604/1834368-20260420172344348-320868519.png" lazyloaded=""  medium-zoom-image"="" style="border: 0px; max-width: 100%; height: auto !important; cursor: zoom-in; transition: transform 300ms cubic-bezier(0.2, 0, 0.2, 1) !important;" /></div></div></div><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">在交互能力上，提供了IM场景的标准能力，业务可以按需使用。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">另外，实际IM场景可能需要一些更为丰富的定制能力，则可以依据方案提供的消息数据源接口、消息交互接口来对具体交互实现进行定制。同时也可以使用相关的交互钩子对交互过程进行干预，以适应自己的业务。</p><h1>8、本文小结</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">本文对团队IM场景的现状做了简单介绍，撇开具体实现细节，就如何搭建一套能够适应多业务需要的通用IM消息流交互层方案，提供了一些思考和实践经验。</p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">从结果来看，该方案稳定支撑了团队多个IM场景，抹除各场景实现差异，有效降低了维护成本和新业务接入成本。</p><h1>9、参考资料</h1><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[1]&nbsp;<a href="http://www.52im.net/thread-3065-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">零基础IM开发入门(一)：什么是IM聊天系统？</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[2]&nbsp;<a href="http://www.52im.net/thread-812-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一套海量在线用户的移动端IM架构设计实践分享(含详细图文)</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[3]&nbsp;<a href="http://www.52im.net/thread-151-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一套原创分布式即时通讯(IM)系统理论架构方案</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[4]&nbsp;<a href="http://www.52im.net/thread-2796-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">从游击队到正规军(二)：马蜂窝旅游网的IM客户端架构演进和实践总结</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[5]&nbsp;<a href="http://www.52im.net/thread-2966-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">社交软件红包技术解密(十)：手Q客户端针对2020年春节红包的技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[6]&nbsp;<a href="http://www.52im.net/thread-4636-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">微信团队分享：来看看微信十年前的IM消息收发架构，你做到了吗</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[7]&nbsp;<a href="http://www.52im.net/thread-4690-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">携程技术分享：亿级流量的办公IM及开放平台技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[8]&nbsp;<a href="http://www.52im.net/thread-4707-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百度公共IM系统的Andriod端IM SDK组件架构设计与技术实现</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[9]&nbsp;<a href="http://www.52im.net/thread-4764-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">转转平台IM系统架构设计与实践(一)：整体架构设计</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[10]&nbsp;<a href="http://www.52im.net/thread-4812-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">一年撸完百万行代码，企业微信的全新鸿蒙NEXT客户端架构演进之路</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[11]&nbsp;<a href="http://www.52im.net/thread-4874-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">转转客服IM聊天系统背后的技术挑战和实践分享</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[12]&nbsp;<a href="http://www.52im.net/thread-4886-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">B站IM消息系统的新架构升级实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[13]&nbsp;<a href="http://www.52im.net/thread-4437-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">企业微信针对百万级组织架构的客户端性能优化实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[14]&nbsp;<a href="http://www.52im.net/thread-3631-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">企业微信的IM架构设计揭秘：消息模型、万人群、已读回执、消息撤回等</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[15]&nbsp;<a href="http://www.52im.net/thread-1470-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">从客户端的角度来谈谈移动端IM的消息可靠性和送达机制</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[16]&nbsp;<a href="http://www.52im.net/thread-1413-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">现代移动端网络短连接的优化手段总结：请求速度、弱网适应、安全保障</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[17]&nbsp;<a href="http://www.52im.net/thread-1998-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM消息ID技术专题(一)：微信的海量IM聊天消息序列号生成实践（算法原理篇）</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[18]&nbsp;<a href="http://www.52im.net/thread-3088-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM开发干货分享：有赞移动端IM的组件化SDK架构设计实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[19]&nbsp;<a href="http://www.52im.net/thread-3615-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">阿里技术分享：闲鱼IM基于Flutter的移动端跨端改造实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[20]&nbsp;<a href="http://www.52im.net/thread-3732-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM开发干货分享：万字长文，详解IM&#8220;消息&#8220;列表卡顿优化实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[21]&nbsp;<a href="http://www.52im.net/thread-4202-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">IM开发干货分享：IM客户端不同版本兼容运行的技术思路和实践总结</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[22]&nbsp;<a href="http://www.52im.net/thread-4359-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">百度统一socket长连接组件从0到1的技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[23]&nbsp;<a href="http://www.52im.net/thread-4470-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">淘宝移动端统一网络库的架构演进和弱网优化技术实践</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[24]&nbsp;<a href="http://www.52im.net/thread-4620-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1;">抖音技术分享：飞鸽IM桌面端基于Rust语言进行重构的技术选型和实践总结</a></p><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">[25]&nbsp;<a href="http://www.52im.net/thread-4633-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">大型IM工程重构实践：企业微信Android端的重构之路</a></p><blockquote style="background-image: none; border-width: medium medium medium 3px; border-style: none none none solid; border-color: currentcolor currentcolor currentcolor #e2dfdf; margin: 10px 0px; background-color: #eeeeee; width: 896px; color: #555555; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif;"><p style="margin: 10px auto;"><strong>即时通讯技术学习：</strong></p><p style="margin: 10px auto;">- 移动端IM开发入门文章：《<a href="http://www.52im.net/thread-464-1-1.html" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">新手入门一篇就够：从零开发移动端IM</a>》</p><p style="margin: 10px auto;">- 开源IM框架源码：<a href="https://github.com/JackJiang2011/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">https://github.com/JackJiang2011/MobileIMSDK</a>（<a href="https://gitee.com/jackjiang/MobileIMSDK" rel="noopener nofollow" target="_blank" style="color: #1d58d1; text-decoration: none;">备用地址点此</a>）</p></blockquote><p style="margin: 10px auto; font-family: &quot;PingFang SC&quot;, &quot;Microsoft YaHei&quot;, &quot;Helvetica Neue&quot;, Helvetica, Arial, sans-serif; background-color: #ffffff;">（<strong>本文同步发布于：</strong><a href="http://www.52im.net/thread-4905-1-1.html" target="_blank" rel="noopener nofollow" style="color: #1d58d1; text-decoration: none;">http://www.52im.net/thread-4905-1-1.html</a>）</p><img src ="http://www.blogjava.net/jb2011/aggbug/451777.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/jb2011/" target="_blank">Jack Jiang</a> 2026-04-20 18:43 <a href="http://www.blogjava.net/jb2011/archive/2026/04/20/451777.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>