2 Star 3 Fork 2

huangshihan / weIm-go

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

分布式websocket服务:weIm-go

语言:Golang

特性

  1. 集群服务注册发现
  2. 分布式连接管理
  3. websocket协议支持各种平台

环境要求

  1. Etcd
  2. Mysql8.0.25+
  3. Redis6.0.9+
  4. ElasticSearch7.12.0+
  5. 七牛云对象存储

运行

  1. 部署elasticsearch、redis、mysql、etcd
  2. 导入 database.sql 到mysql
  3. 修改配置文件config.yml
  4. go run main.go

分布式部署

  1. 启动多个节点服务
  2. nginx负载均衡

pprof性能监控访问

  1. 配置项:http.pprof
  2. web访问:http://ip:port/debug/pprof

websocket并发性能测试

执行并发测试:go run ./test/ws.client.go; 建议linux下进行并发测试,默认并发10000个客户端连接websocket并发送认证消息,可手动修改并发客户端数量。 测试环境: i5-1035G4,WIN10系统 测试结果: 3万连接广播消息耗时平均0.8s,占用内存1.2G,测试路由:/ws/sendToAll 仅供参考,请自行部署测试。

API接口列表

  1. 用户登录
  2. 用户注册
  3. 设置昵称
  4. 设置头像
  5. 实名认证
  6. 上传用户位置(经纬度)
  7. 附近的人
  8. 搜索用户
  9. 好友列表
  10. 添加好友
  11. 添加好友请求列表
  12. 同意/拒绝好友请求
  13. 删除好友
  14. 发送好友消息
  15. 撤回好友消息
  16. 创建群组
  17. 搜索群组
  18. 我的群组
  19. 申请加群
  20. 申请加群请求列表
  21. 同意/拒绝加群请求
  22. 退出群组
  23. 踢出群组
  24. 发送群组消息
  25. 撤回群组消息
  26. 上传聊天附件:语音、视频、文件格式

文档说明

一、Api接口

登录:/api/v1/login 参数:username string 参数:password string

注册:/api/v1/register 参数:username string 参数:password string

登录/注册成功后会返回token,其他接口和websocket授权都需要带上token;api 接口在header添加 Login-Token 字段,websocket在连接后发送一个token认证信息进行认证,15秒之内没有认证成功会强制断开连接。

二、websocket长连接

连接地址:ws://ip:port/ws(例如ws://127.0.0.1:8080/ws) 登录成功后进行连接认证:

通信消息JSON格式

{
    "Cmd": "指令",
    "FromId": "来源id",
    "ToId": "接收id",
    "Ope": "消息通道",
    "Type": "消息类型",
    "Body": "消息内容"
}
字段说明:
Cmd:指令:
    CmdFail 2 通用失败
    CmdSign 3 登录
    CmdSignSuccess 4 登录成功
    CmdReceiveFriendMessage 6 收到好友消息
    CmdWithdrawFriendMessage 7 撤回好友消息
    CmdReceiveFriendAdd 8 收到好友添加请求
    CmdReceiveFriendAddResult 9 收到好友请求结果
    CmdReceiveGroupMessage 10 收到群消息
    CmdWithdrawGroupMessage 11 撤回群消息
    CmdReceiveGroupJoin 12 收到加入群组请求
    CmdReceiveGroupJoinResult 13 收到加入群组结果
    CmdReceiveGroupShot 14 收到被踢出群组通知
FromId:消息发送方id
ToId:消息接收方id,ope=0时为用户id,ope=1是为群组id
Ope:消息通道,OpeFriend 0好友消息,OpeGroup 1群消息, OpeSystem 2系统消息
Type:消息类型,
    TypeText 0 文本消息,
    TypePicture 1 图片,
    TypeVoice 2 语音,
    TypeVideo 3 视频,
    TypeGeo 4 地理位置信息,
    TypeFile 6 文件,
    TypePrompt10 提示消息
Body:消息内容,登录认证时填用户Token

用户连接websocket后发送登录认证消息

{
    "Cmd": 3,
    "FromId": 0,
    "ToId": 0,
    "Ope": 0,
    "Type": 0,
    "Body": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdCI6MTYxOTE0Mzg0OSwiaWQiOjV9.KQ7dOv6bE_fP5NpMehziesFMsZXDAdVrbYBHyZROw40"
}

登录成功会收到消息:

{
    "Cmd": 4,
    "FromId": 0,
    "ToId": 0,
    "Ope": 0,
    "Type": 0,
    "Body": "认证成功"
}
MIT License Copyright (c) 2021 黄石汉 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

分布式社交服务websocket:weIm-go gin+gorm+jwt+grpc 展开 收起
Go 等 2 种语言
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Go
1
https://gitee.com/huangshihan200916/weIm-go.git
git@gitee.com:huangshihan200916/weIm-go.git
huangshihan200916
weIm-go
weIm-go
master

搜索帮助