1 Star 0 Fork 0

yemuysy / my_im

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

my_im

介绍

设计:

消息报文:request \ ack \ notify
    - R:客户端主动发送给服务器的报文
    - A:服务器被动应答客户端的报文,一个A一定对应一个R
    - N:服务器主动发送给客户端的报文

消息报文:request \ ack \ notify
    - R:客户端主动发送给服务器的报文
    - A:服务器被动应答客户端的报文,一个A一定对应一个R
    - N:服务器主动发送给客户端的报文

缓存:用户状态:online \ offline

消息去重:客户端根据消息id来去重

消息时序性:消息id的生成方式由会话id+sequence,sequence通过自增保证消息时序性,
    每次发送,需要先获取到消息id,可以客户端获取,也可以服务端自己获取
    同时为了避免服务重启,sequece 丢失,需要持久化,持久化常见问题不是实时的,
    可以设置个当前的 max_sequece 最大消息id 可以为100递增,当sequece> max_sequece/1.25时
    进行递增,后面重启服务后max_sequece 覆盖 sequence,
    只要保证每次获取消息id都比前一个大就行了

存在问题;
    msg:A 发送失败时,要终止之后的转发操作
    
    消息风暴扩散系数:是指一个消息发出时,变成N个消息的扩散系数,
        这个系数与业务及数据相关,一定程度上它的大小决定了技术采用推送还是拉取   

消息发送:
    私聊:clientA \ imServer \ clientB
    设计表:
    user_msg(id, msgId, sender, receiver, time) id, 消息id, 发送人, 接收人 , 发送时间
    
    A -> B 发送你好 A不用管收到消息了没有
    在线情况:
        1. clientA 向 imServer 发送"msg:R"
        2. imServer 收到并保存消息后,回复 "msg:A",此时 clientA 知道已经发送成功
        3. imServer 向 clientB 发送 "msg:N"
        4. clientB 收到后回复 imServer "ack:N"
    离线情况:
        1. clientA 向 imServer 发送"msg:R"
        2. imServer 收到后,回复 "msg:A"
        3. 写入离线库,上线进行分发,主动推送,
            采用分页推送,每次推送成功后删除离线消息表
            之后再次查看分页数据,不要一次全查,全查可能导致问题是数据丢失
            数据重发不可怕,在客户端做去重标识
    
    群聊:clientA \ clientB \ clientC \ gruopA
        设计表:这里采用读扩张,就是只写一条数据,然后分发给不同的用户的时候,在进行扩展开来
            写扩展指的是给发送一条数据给群里的人都保存一份
        成员表
        user_group_members(id, group_id, uid) id,群id,用户id
        群消息表
        user_group_msg(id, msgId, group_id, sender, sendingTime, msgDetail) id, 消息id, 群id, 发送人id, 发送时间, 消息内容
        离线群消息表
        user_group_off_msg(id, groupId, userId, msgId)
    
        A -> groupA -> B\C
        发送情况:无论在不在线,都先往离线表里面插入数据,再发送
            1. clientA 向 imServer 发送"msg:R"
            2. imServer 保存群消息后 向 clientA 发送 "msg:A"
            3. imServer 获取 groupA 的群成员,并未每个人都写入离线表里面
            4. 开始向群成员 clienB\clientC 推送消息
                1. 用户在线发送 "msg:N"
                2. 用户不在线,则不发,等用户重写上线,再拉取消息
            5. 用户收到消息后 回复 "ack:N",服务器删除离线消息
        
        设计表2
        成员表
        user_group_members(id, group_id, uid, last_ack_msg_id/lastSendTime) id,群id,用户id,最后一次收到"ack:N"的消息id或者时间
        群消息表
        user_group_msg(id, msgId, group_id, sender, sendingTime, msgDetail) id, 消息id, 群id, 发送人id, 发送时间, 消息内容
        这里不在需要离线消息表了,每次发送消息成功回调的 ack:N 就可以把这个设置成了最后一条收到的群消息,
        之后登录的时候,就可以直接拉取这张表之后的消息内容
        弊端就是重复推送可能比较多,需要前端去重,前端消息去重无论什么时候都是必须的,
        还有就是 ack:N 后要频繁修改数据库,需要做缓存来做同步

        群消息批量ACK问题:
        每收到一条群消息就ack一次的话,会给系统带来太大的压力,这里推荐的就是批量ack,每收到多少条就执行ack+时间限制
        比如:10秒内收到超过10条消息就ack一次,10秒后收不到10条消息也ack一次
        
        群离线消息过多,采取分页推送

成员在线:
1. 好友状态提醒:同步推送/或者轮询
clientA -> imServer -> clientB
clientA 下线监听,imServer 主动推送给加了ClientA的人(有可能我删除好友,对方还存在)

2. 群好友在线情况:按需拉取
当主动触发某个群的时候再去判断这个群好友的情况,点击进来聊天就立马触发后加轮询

集群:
    私聊不在一台机器上,通过订阅本路由 listener+addr+host 通过mq转发到对的的机器
    群聊:listener 通过mq订阅,群消息只处理本地在的路由

消息体:下述的单条消息中包括两个文本消息元素和一个表情元素,消息元素顺序是文本+表情+文本。

    "MsgBody": [
        {
            "msgType": "1", 
            "msgContent": {
                "text": "hello"
            }
        }, 
        {
            "msgType": "5", 
            "msgContent": {
                "index": 1, 
                "data": "content"
            }
        }, 
        {
            "msgType": "1", 
            "msgContent": {
                "text": "world"
            }
        }
    ]
}
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

即时通信后台 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Java
1
https://gitee.com/yemuysy/my_im.git
git@gitee.com:yemuysy/my_im.git
yemuysy
my_im
my_im
master

搜索帮助