1 | # LeanCloud JavaScript Realtime SDK
|
2 |
|
3 | [![npm](https://img.shields.io/npm/v/leancloud-realtime.svg?style=flat-square)](https://www.npmjs.com/package/leancloud-realtime)
|
4 | [![npm](https://img.shields.io/npm/v/leancloud-realtime/next.svg?style=flat-square)](https://www.npmjs.com/package/leancloud-realtime)
|
5 | ![gzip size](https://img.badgesize.io/leancloud/js-realtime-sdk/next-dist/dist/realtime-browser.min.js.svg?compression=gzip&style=flat-square)
|
6 | [![Build Status](https://img.shields.io/travis/leancloud/js-realtime-sdk.svg?style=flat-square)](https://travis-ci.org/leancloud/js-realtime-sdk)
|
7 | [![Codecov](https://img.shields.io/codecov/c/github/leancloud/js-realtime-sdk.svg?style=flat-square)](https://codecov.io/github/leancloud/js-realtime-sdk)
|
8 | [![Known Vulnerabilities](https://snyk.io/test/github/leancloud/js-realtime-sdk/badge.svg?style=flat-square)](https://snyk.io/test/github/leancloud/js-realtime-sdk)
|
9 |
|
10 | 为您的 JavaScript App 接入 LeanCloud 实时通讯服务。
|
11 |
|
12 | ## 版本说明
|
13 |
|
14 | 遵循 [语义化版本](http://semver.org/lang/zh-CN/)。
|
15 |
|
16 | 安装稳定版本:
|
17 |
|
18 | ```
|
19 | npm install leancloud-realtime --save
|
20 | ```
|
21 |
|
22 | 安装测试版本:
|
23 |
|
24 | ```
|
25 | npm install leancloud-realtime@next --save
|
26 | ```
|
27 |
|
28 | 安装指定版本:
|
29 |
|
30 | ```
|
31 | // 安装 v3 版本
|
32 | npm install leancloud-realtime@3 --save
|
33 | ```
|
34 |
|
35 | ## 支持的运行环境
|
36 |
|
37 | - 浏览器 / WebView
|
38 | - IE 10+
|
39 | - Edge latest
|
40 | - Chrome 45+
|
41 | - Firefox latest
|
42 | - iOS 9.3+
|
43 | - Android 4.4+
|
44 | - Node.js 4.0+
|
45 | - 微信小程序/小游戏 latest
|
46 | - React Native 0.26+
|
47 | - Electron latest
|
48 |
|
49 | ## 文档
|
50 |
|
51 | - [安装文档](https://leancloud.cn/docs/sdk_setup-js.html)
|
52 | - [使用文档](https://leancloud.cn/docs/realtime_guide-js.html)
|
53 | - [API 文档](https://leancloud.github.io/js-realtime-sdk/docs/)
|
54 |
|
55 | ## Demo
|
56 |
|
57 | - [Simple Chatroom](https://leancloud.github.io/js-realtime-sdk/demo/simple-chatroom/) ([src](https://github.com/leancloud/js-realtime-sdk/tree/master/demo/simple-chatroom))
|
58 | - [LeanMessage](https://leancloud.github.io/leanmessage-demo) ([src](https://github.com/leancloud/leanmessage-demo))
|
59 | - [WebRTC 视频通话](https://leancloud.github.io/js-realtime-sdk/demo/webrtc/) ([src](https://github.com/leancloud/js-realtime-sdk/tree/master/demo/webrtc))
|
60 |
|
61 | ## 插件
|
62 |
|
63 | | package name | 描述 | 版本 | 文档 |
|
64 | | :------------------------------------------- | :------------ | :-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------: | :--------------------------------------------------------------------------------------: |
|
65 | | leancloud-realtime-plugin-typed-messages | 富媒体消息 | [![npm](https://img.shields.io/npm/v/leancloud-realtime-plugin-typed-messages.svg?style=flat-square)](https://www.npmjs.com/package/leancloud-realtime-plugin-typed-messages) | [API docs](https://leancloud.github.io/js-realtime-sdk/plugins/typed-messages/docs/) |
|
66 | | leancloud-realtime-plugin-groupchat-receipts | 群聊已读回执 | [![npm](https://img.shields.io/npm/v/leancloud-realtime-plugin-groupchat-receipts.svg?style=flat-square)](https://www.npmjs.com/package/leancloud-realtime-plugin-groupchat-receipts) | [API docs](https://leancloud.github.io/js-realtime-sdk/plugins/groupchat-receipts/docs/) |
|
67 | | leancloud-realtime-plugin-webrtc | WebRTC 客户端 | [![npm](https://img.shields.io/npm/v/leancloud-realtime-plugin-webrtc.svg?style=flat-square)](https://www.npmjs.com/package/leancloud-realtime-plugin-webrtc) | [API docs](https://leancloud.github.io/js-realtime-sdk/plugins/webrtc/docs/) |
|
68 |
|
69 | ## 支持
|
70 |
|
71 | - 在使用过程中遇到了问题时
|
72 | - 如果你是商用版用户,请新建一个工单。
|
73 | - 也可以在 [论坛](https://forum.leancloud.cn/) 提问、讨论。
|
74 |
|
75 | ## 贡献
|
76 |
|
77 | 如果你希望为这个项目贡献代码,请按以下步骤进行:
|
78 |
|
79 | 1. Fork 这个项目,clone 到本地
|
80 | 1. 在目录中执行 `npm install` 安装所需 Node.js 依赖包
|
81 | 1. 编码,更新测试用例
|
82 | 1. 运行 `npm test` 确保测试全部 pass
|
83 | 1. 提交改动,请遵循 [conversational commit message 风格](http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html)
|
84 | 1. 发起 Pull Request 至 master 分支
|
85 |
|
86 | ### 项目的目录结构
|
87 |
|
88 | ```
|
89 | .
|
90 | ├── demo
|
91 | ├── deploy.sh // 部署 gh-pages 分支
|
92 | ├── release.sh // 部署 dist 分支
|
93 | ├── dist // 打包产出 (dist 分支)
|
94 | │ ├── realtime-browser.js // 浏览器用
|
95 | │ ├── realtime-browser.min.js // 浏览器用(uglified)
|
96 | │ ├── realtime-weapp.js // 微信小程序用
|
97 | │ └── realtime.js // node 用
|
98 | ├── proto
|
99 | │ ├── message-compiled.js // 使用 pbjs 生成的 message 类
|
100 | │ ├── message.js // ES6 wrapper
|
101 | │ └── message.proto // proto 原始文件
|
102 | ├── src // 源码
|
103 | │ └── index.js // 打包入口
|
104 | ├── test // 测试用例
|
105 | │ ├── browser // 浏览器测试入口
|
106 | │ └── index.js // 测试入口
|
107 | └── plugins
|
108 | ├── typed-messages // leancloud-realtime-plugin-typed-messages package
|
109 | └── webrtc // leancloud-realtime-plugin-webrtc package
|
110 | ```
|
111 |
|
112 | ### Architecture
|
113 |
|
114 | SDK 分为连接层与应用层两部分,只存在应用层对连接层公开 API 的调用,连接层对开发者不可见。
|
115 |
|
116 | #### 连接层
|
117 |
|
118 | - `WebSocketPlus`:封装了 WebSocket。相比 w3 WebSocket,增加了以下特性:
|
119 | - 是一个有限状态机
|
120 | - 实现了 [Node.js EventEmitter 接口](https://nodejs.org/api/events.html)
|
121 | - 超时与自动重连机制
|
122 | - url 参数支持 Promise 及备用地址
|
123 | - `Connection`:继承自 `WebSocketPlus`,增加了与业务相关的功能:
|
124 | - 根据 subprotocol 自动处理发送与接收的消息,应用层发送接收的均是 ProtoBuf Message 类
|
125 | - `send` 接口返回 Promise,在 server 回复后才算 send 成功
|
126 | - 实现了应用层 ping/pong
|
127 |
|
128 | #### 应用层
|
129 |
|
130 | - `Realtime`:开发者使用 SDK 的入口,负责访问 router、创建 connection、创建与管理 clients、创建 messageParser(管理消息类型)、监听 connection 的消息并 dispatch 给对应的 client
|
131 | - `Client`:所有的 clients 共享一个 connection
|
132 | - `IMClient`:对应即时通讯中的「用户」,持有 connection 与 conversations,负责创建管理将收到的消息处理后在对应 conversation 上派发,所有的 IMClients 共享一个 messageParser
|
133 | - `MessageParser` 消息解析器,负责将一个 JSON 格式的消息 parse 为对应的 Message 类
|
134 | - `Conversation`:实现对话相关的操作
|
135 | - `ConversationQuery`:对话查询器
|
136 | - `Messages`
|
137 | - `AVMessage`:接口描述,生成文档用
|
138 | - `Message`:消息基类
|
139 | - `TypedMessage`:类型消息基类,继承自 `Message`
|
140 | - `TextMessage`:文本消息,继承自 `TypedMessage`
|
141 | - 其他富媒体消息类(`FileMessage` 及其子类、`LocationMessage`)由于依赖 leancloud-storage,作为另一个独立 package 发布
|
142 |
|
143 | ### 开启调试模式
|
144 |
|
145 | #### Node.js
|
146 |
|
147 | ```bash
|
148 | export DEBUG=LC*
|
149 | ```
|
150 |
|
151 | #### 浏览器
|
152 |
|
153 | ```javascript
|
154 | localStorage.setItem('debug', 'LC*');
|
155 | ```
|
156 |
|
157 | ## Develop Workflow
|
158 |
|
159 | ### 本地开发
|
160 |
|
161 | 更新 .proto 后请运行
|
162 |
|
163 | ```
|
164 | npm run convert-pb
|
165 | ```
|
166 |
|
167 | 测试
|
168 |
|
169 | ```
|
170 | npm run test:node -- --grep KEYWORDS
|
171 | ```
|
172 |
|
173 | 浏览器测试
|
174 |
|
175 | ```
|
176 | npm run test:browser-local
|
177 | ```
|
178 |
|
179 | 编译
|
180 |
|
181 | ```
|
182 | npm run build
|
183 | ```
|
184 |
|
185 | ### 持续集成
|
186 |
|
187 | 合并 PR 到 master 分支后持续集成会自动运行 `npm build` 与 `npm run docs`,然后将 dist 目录推送到 dist 分支,将文档与 demo 推送到 gh-pages。
|
188 |
|
189 | ## Release Process Workflow
|
190 |
|
191 | 0. 遵循 semver 提升 `package.json` 中的版本号
|
192 | 1. `npm run changelog` 生成新的 `changelog.md`,润色之
|
193 | 1. Commit `package.json`,`changelog.md`
|
194 | 1. Push to remote `master` branch
|
195 | 1. 等待持续集成 pass
|
196 | 1. 使用 GitHub 基于 dist 分支生成 pre-release 包(for bower)
|
197 | 1. Fetch and checkout remote `dist` branch 并确认该提交的内容是即将发布的版本
|
198 | 1. npm publish(`npm publish`,需 npm 协作者身份),如果是 pre-release 版本需要带 next tag
|
199 | 1. 如有更新,在 npm 上发布各个 plugin
|