@realsee/live 0.7.4 → 0.7.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ## 功能说明
6
6
 
7
- **Live** 是 [如视开发者中心 VRTC 服务](https://developers.realsee.com/docs/#/docs/live/README) 的核心客户端 Web SDK,搭配 Live Container
7
+ **Live** 是 [如视开放平台线上带看](https://open-platform.realsee.com/developer/docs/live/intro/) 的核心客户端 Web SDK,搭配 Live Container
8
8
  SDKs(iOS/Android/微信小程序)和 Live API 可以快速实现实时同屏、实时语音、IM 等功能。
9
9
 
10
10
  ### 名词释义
@@ -39,23 +39,36 @@ yarn add @realsee/live
39
39
 
40
40
  简单使用样例(先不考虑实时语音能力):
41
41
 
42
- ```js
42
+ ```typescript
43
43
  import { createLive } from '@realsee/live'
44
44
 
45
- const live = createLive({
45
+ // 定义帧数据类型(可选,但推荐使用 TypeScript)
46
+ interface MyKeyframes {
47
+ camera: { position: [number, number, number] }
48
+ annotation: { text: string; position: [number, number] }
49
+ }
50
+
51
+ const live = createLive<MyKeyframes>({
46
52
  url: 'wss://ws-access.realsee.com/***/?=xxx' /* 连接 WebSocket 服务的 URL */,
47
53
  getTicket: async () => {
48
54
  /* 获取带看"门票"的回调函数 */
55
+ const response = await fetch('/api/get-ticket')
56
+ return response.json().ticket
49
57
  },
50
58
  })
59
+
60
+ // 连接带看
61
+ await live.connect()
51
62
  ```
52
63
 
53
64
  在创建 `live` 实例时配置了两个参数:
54
65
 
55
- - `url`:连接 WebSocket 服务的 URL,如果在创建 `live` 实例阶段并不知道 URL ,您可以在合适的时机通过 `live.connect({ url })` 方法进行 WebScoket 连接。
56
- - `getTicket()`:是一个异步回调函数,其返回值是连接 WebScoket 服务的 **"门票"**,关于如何获取 `ticket` 请查阅 [VR 带看 API](https://developers.realsee.com/docs/#/docs/live/server/README)。
66
+ - `url`:连接 WebSocket 服务的 URL(可选),如果在创建 `live` 实例阶段并不知道 URL,您可以在合适的时机通过 `live.connect({ url })` 方法进行 WebSocket 连接。
67
+ - `getTicket()`:是一个异步回调函数(可选),其返回值是连接 WebSocket 服务的 **"门票"**,关于如何获取 `ticket` 请查阅 [如视开放平台带看文档](https://open-platform.realsee.com/developer/docs/live/intro/)。也可以在 `connect` 时提供。
68
+
69
+ > **提示**:如果在创建实例时同时提供了 `url` 和 `getTicket`,Live 会自动尝试连接,无需手动调用 `connect()` 方法。
57
70
 
58
- > 关于 `createLive()` 函数的更多参数配置请参考 [LiveOptions](https://unpkg.com/@realsee/live/docs/interfaces/live.LiveOptions.html) 。
71
+ > 关于 `createLive()` 函数的更多参数配置请参考 [LiveOptions](https://unpkg.com/@realsee/live/docs/interfaces/live.LiveOptions.html) 文档。
59
72
 
60
73
  获取实例 `live` 之后,可以做很多有趣的事情。但在讲述详细用途之前,先简单介绍下 **Live** 中的 **消息系统**。
61
74
 
@@ -78,12 +91,12 @@ const live = createLive({
78
91
 
79
92
  以 [Five](https://developers.realsee.com/docs/#/docs/five/web/quickstart) 同屏为例
80
93
 
81
- ```js
94
+ ```typescript
82
95
  // 发送本地 帧数据 (Five State)
83
96
  live.sendKeyframe('five', { panoIndex: 13 })
84
97
 
85
98
  // 接收远程 帧数据 (Five State)
86
- live.keyframes.on('five', (newState, prevState) => {
99
+ live.keyframes.on('five', (newState, prevState, frontRequestId) => {
87
100
  // 可以拿最新的 `newState` 更新本地 UI 状态
88
101
  })
89
102
  ```
@@ -94,61 +107,65 @@ live.keyframes.on('five', (newState, prevState) => {
94
107
 
95
108
  **Live** 有会话房间的概念,当您成功连接 WebSocket 服务之后等同于您作为一个用户加入了一个房间。此房间用户除了您之外还会有其他用户,您可以通过如下方式获取或更新用户信息:
96
109
 
97
- ```js
110
+ ```typescript
98
111
  // 属性:获取本地用户信息
99
112
  live.selfInfo
100
113
 
101
114
  // 属性:获取当前房间内所有用户列表
102
115
  live.userList
103
116
 
117
+ // 属性:获取房间信息
118
+ live.roomInfo
119
+
104
120
  // 方法:更新本地用户信息
105
121
  live.setSelfInfo({})
106
122
 
107
123
  // 事件:本地用户信息发生变更
108
- live.on('selfInfoUpdate', (userInfo) => {})
124
+ live.on('selfInfoUpdate', (userInfo, frontRequestId) => {})
109
125
 
110
126
  // 事件:房间用户列表信息发生变更
111
- live.on('userListUpdate', (userList) => {})
127
+ live.on('userListUpdate', (userList, frontRequestId) => {})
112
128
  ```
113
129
 
114
130
  ### 广播消息
115
131
 
116
132
  除了帧数据和房间信息之外,还可以用事件的形式向房间内的其他用户广播消息:
117
133
 
118
- ```js
119
- live.broadcast(data /* 广播的数据 */, ['user_id_1', 'user_id_2'] /* 收到此用户的userId */)
134
+ ```typescript
135
+ live.broadcast(
136
+ data /* 广播的数据 */,
137
+ ['user_id_1', 'user_id_2'] /* 收到此消息的用户ID列表 */,
138
+ 0 /* 超时时间(毫秒),0 表示不超时 */
139
+ )
120
140
  ```
121
141
 
122
142
  此时,房间内用户 Id 为 `user_id_1`、`user_id_2` 的用户会收到此广播消息(其他用户无感知):
123
143
 
124
- ```js
125
- live.on('broadcast', (data /* 广播的数据 */) => {})
144
+ ```typescript
145
+ live.on('broadcast', (data /* 广播的数据 */, frontRequestId) => {})
126
146
  ```
127
147
 
128
148
  更多的例子请查看 [Live API](https://unpkg.com/@realsee/live/docs/index.html) 文档 。
129
149
 
130
150
  ## 事件监听
131
151
 
132
- **Live** 提供 `WebSocket 事件`、`live 事件` 和 `RTC 事件` 三类异步事件的监听。
152
+ **Live** 提供 `带看事件` 和 `RTC 事件` 两类异步事件的监听。
133
153
 
134
- 其中, `WebSocket 事件`、`live 事件` 都可以通过 `live.on('/*事件名*/', /*事件回调*/)` 的方式监听。
154
+ 所有事件都可以通过 `live.on('/*事件名*/', /*事件回调*/)` 的方式监听。
135
155
 
136
- 比如监听 WebSocket 状态发生变更:
156
+ 比如监听连接状态变化:
137
157
 
138
- ```ts
139
- live.on('ws.readyStateUpdate', (readyState: WebSocketState) => {
140
- console.log(readyState) // readyState 即是最新的 WebScoket 状态
158
+ ```typescript
159
+ live.on('stateChange', (state, prevState) => {
160
+ console.log(`状态从 ${prevState} 变为 ${state}`)
141
161
  })
142
162
  ```
143
163
 
144
- ### WebSocket 事件
145
-
146
- - `ws.beforeConnect(reConnect?: number): void` WebSocket 完成连接之后。
147
- - `ws.afterConnect(reConnect?: number): void` WebSocket 完成连接之前。
164
+ ### 带看连接状态
148
165
 
149
- - `ws.readyStateUpdate(readyState: WebSocketState): void` WebSocket 完成状态发生变更。
166
+ **Live** 前后端服务交互是基于 WebSocket 长链接的。一次带看成功进入、退出的标识由连接状态 `OPEN`、`CLOSED` 体现。因此,所有 **VRTC** 相关的事件处理逻辑应保证在 `OPEN` 状态之后进行。
150
167
 
151
- **WebSocket 连接状态枚举表:**
168
+ **连接状态枚举表:**
152
169
 
153
170
  | 状态名称 | 状态描述 |
154
171
  | -------------- | ---------- |
@@ -158,47 +175,84 @@ live.on('ws.readyStateUpdate', (readyState: WebSocketState) => {
158
175
  | CLOSING | 关闭中 |
159
176
  | CLOSED | 关闭成功 |
160
177
 
161
- **Live** 前后端服务交互是基于 WebSocket 长链接的。一次带看成功进入、退出的标识由 WebSocket 的状态 `OPEN`、`CLOSED`体现。 因此,所有 **VRTC** 相关的事件处理逻辑应保证在`OPEN`
162
- 事件之后进行。
178
+ ### 带看事件
179
+
180
+ - `stateChange(state: LiveState, prevState?: LiveState): void` - 带看连接状态变化事件
181
+
182
+ - `builtinEvent(builtinMsg: BuiltinMsg): void` - 接收服务端推送的内置事件消息(包括用户变化和房间变化)
183
+
184
+ - `userChange(builtinMsg: BuiltinMsg): void` - 用户信息改变事件(是 builtinEvent 的子集,event_type 为 UserChange)
185
+
186
+ - `roomChange(builtinMsg: BuiltinMsg): void` - 房间信息改变事件(是 builtinEvent 的子集,event_type 为 RoomChange)
163
187
 
164
- ### live 事件
188
+ - `broadcast(evtMsg: Record<string, any>, frontRequestId: string): void` - 接收其他用户的广播消息
165
189
 
166
- - `broadcast(evtMsg: Record<string, any>, frontRequestId: string): void` 接收其他用户的广播消息。
190
+ - `keyframes(keyframes: Partial<Snapshot>, frontRequestId: string): void` - 接收其他用户的帧数据
167
191
 
168
- - `builtinEvent(builtinMsg: BuiltinMsg): void` 接收服务端推送的内置事件消息。
192
+ - `readyKeyframeSync(lastKeyframe: Partial<Snapshot>): void` - 表明帧同步行为已就绪(**已废弃**,请使用 `keyframes` 事件)
169
193
 
170
- - `keyframes(keyframes: Partial<Snapshot>, frontRequestId: string): void` 接收其他用户的帧数据。
194
+ - `selfInfoUpdate(userInfo: UserInfo, frontRequestId: string): void` - 自身的信息发生变更
171
195
 
172
- - `readyKeyframeSync(lastKeyframe: Partial<Snapshot>): void` 表明帧同步行为已就绪。
196
+ - `userListUpdate(userList: UserInfo[], frontRequestId: string): void` - 用户列表信息发生变更
173
197
 
174
- - `selfInfoUpdate(userInfo: UserInfo, frontRequestId: string): void` 自身的信息发生变更。
198
+ - `kickOut(): void` - 被踢出房间
175
199
 
176
- - `userListUpdate(userList: UserInfo[], frontRequestId: string): void` 用户列信息表发生变更。
200
+ - `error(liveMsg: LiveMsg): void` - 服务端推送出现异常
177
201
 
178
- `live` 实例提供了很多诸如`live.connect()`、`live.broadcast()`、`live.sendKeyframe()`等方法,这些方法都是异步反馈且可能会影响房间内的其他用户。
202
+ `live` 实例提供了很多诸如`live.connect()`、`live.broadcast()`、`live.sendKeyframe()`、`live.exit()`、`live.close()`、`live.dispose()`等方法,这些方法都是异步反馈且可能会影响房间内的其他用户。
179
203
  您可以通过监听以上事件来获知这些影响。
180
204
 
181
- 需要注意的是 `readyKeyframeSync` 事件是可进行帧同步的节点标识,收到此事件后您的帧同步行为才生效。
205
+ ### 主要方法说明
206
+
207
+ - `connect(options?)` - 连接带看房间,建立 WebSocket 连接
208
+ - `exit()` - 退出带看房间(不发送房间关闭消息)
209
+ - `close()` - 关闭带看房间(会发送房间关闭消息,通知服务器)
210
+ - `dispose()` - 销毁带看实例,清理所有资源(事件监听、Yjs 文档等)
211
+ - `sendKeyframe(key, frame)` - 发送帧数据
212
+ - `getFramesByKey(key)` - 根据键获取对应的帧数据
213
+ - `broadcast(data, toUserIds, timeout?)` - 向指定用户广播消息
214
+ - `setSelfInfo(userInfo)` - 更新本地用户信息
215
+ - `toggleMicro(status)` - 切换麦克风状态
216
+ - `kick(toUserIds)` - 踢出指定用户
217
+ - `forbidMicro(status, toUserIds?)` - 禁止/允许指定用户的麦克风
218
+ - `forbidSync(sync, toUserIds?)` - 禁止/允许指定用户的帧数据同步
219
+ - `updateStatus(evtType, atnType, status, toUserIds?)` - 更新带看状态(用户或房间状态)
220
+
221
+ ### 主要属性说明
222
+
223
+ - `state` - 当前带看连接状态(LiveState)
224
+ - `readyState` - WebSocket 就绪状态
225
+ - `url` - WebSocket 连接 URL
226
+ - `options` - 配置选项(只读)
227
+ - `snapshot` - 当前所有帧数据的快照
228
+ - `selfInfo` - 本地用户信息
229
+ - `userList` - 房间内所有用户列表
230
+ - `roomInfo` - 房间信息
231
+ - `$RTC` - RTC 实例(用于语音通信)
232
+ - `jsBridge` - JSBridge 实例(用于 App/WebView 环境)
233
+ - `keyframes` - 帧数据监听器(Subscribe 实例)
182
234
 
183
235
  ### RTC 事件
184
236
 
185
- - `error(error: Error): void` 异常事件。
237
+ RTC 相关事件通过 `live.$RTC.on('/*事件名*/', /*事件回调*/)` 方式来监听。
186
238
 
187
- - `initWillStart(): void` 即将初始化事件。
239
+ - `error(error: Error): void` - 异常事件
188
240
 
189
- - `inited(): void` 初始化完成事件。
241
+ - `initWillStart(): void` - 即将初始化事件
190
242
 
191
- - `joinWillStart(): void` 即将加入语音房间事件。
243
+ - `inited(): void` - 初始化完成事件
192
244
 
193
- - `joined(): void` 成功加入语音房间事件。
245
+ - `joinWillStart(): void` - 即将加入语音房间事件
194
246
 
195
- - `userVolumes(userVolumes: UserVolume[]): void` 房间内每个说话用户的音量。
247
+ - `joined(info: RTCJoinedInfo): void` - 成功加入语音房间事件
196
248
 
197
- - `weakNetwork(): void` 弱网提醒事件。
249
+ - `userVolumes(userVolumes: UserVolume[]): void` - 房间内每个说话用户的音量
198
250
 
199
- `live.$RTC` 实例需满足 [RTC 协议](https://unpkg.com/@realsee/live/docs/interfaces/rtc.RTCProtocol.html),其事件通过 `live.$RTC.on('/*事件名*/', /*事件回调*/)` 方式来监听,比如监听语音房间内正在说话的用户及其音量:
251
+ - `weakNetwork(): void` - 弱网提醒事件
200
252
 
201
- ```ts
253
+ 比如监听语音房间内正在说话的用户及其音量:
254
+
255
+ ```typescript
202
256
  live.$RTC.on('userVolumes', (userVolumes: UserVolume[]) => {
203
257
  console.log(userVolumes) // userVolumes 正在说话的用户及其音量数据。
204
258
  })
@@ -206,30 +260,17 @@ live.$RTC.on('userVolumes', (userVolumes: UserVolume[]) => {
206
260
 
207
261
  ### 异常处理
208
262
 
209
- - `ws.error(error: WebSocketError): void` WebSocket 连接出现异常。
210
-
211
- **WebSocket 错误状态枚举表:**
212
-
213
- | 错误字段 | 错误描述 | 触发场景 |
214
- | ---------------- | ----------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------- |
215
- | Close | 错误类型枚举参照[CloseEvent](https://developer.mozilla.org/zh-CN/docs/Web/API/CloseEvent) | 错误原因参照[CloseEvent](https://developer.mozilla.org/zh-CN/docs/Web/API/CloseEvent) |
216
- | Error |
217
- | MicroAuth | 麦克风授权错误 | 语音功能获取麦克风授权失败 |
218
- | DuplicateConnect | 重复连接 | 使用相同的 WS 链接申请连接 |
219
- | IllegalURL | 非法的 WS 链接 | 传入的 WS 链接不合法 |
220
- | Unknown | 未知错误 | 其他未被枚举的错误原样返回 |
221
-
222
- - `error(liveMsg: LiveMsg): void` 服务端推送出现异常。
263
+ - `error(liveMsg: LiveMsg): void` - 服务端推送出现异常。
223
264
 
224
265
  与 WebSocket 通信的结构体`LiveMsg`格式如下:
225
266
 
226
267
  | 名称 | 类型 | 描述 |
227
268
  | --------------- | ------------------- | ----------------------------------------------------------------------------------------------------- |
228
- | appId | string |
229
- | code | string | 返回码,详情参照[服务端 API Code 码表](https://developers.realsee.com/docs/#/docs/live/server/README) |
269
+ | appId | string | 应用ID |
270
+ | code | string | 返回码,详情参照[如视开放平台带看文档](https://open-platform.realsee.com/developer/docs/live/intro/) |
230
271
  | command | 指令 | WebSocket 通信指令类型,详情请查看[Live API](https://unpkg.com/@realsee/live/docs/index.html) 文档 |
231
272
  | data | Record<string, any> | 本条指令对应的业务数据 |
232
- | frontRequestId: | string | 前端的请求 ID,由 WS 服务原样透传回前端 |
273
+ | frontRequestId | string | 前端的请求 ID,由 WS 服务原样透传回前端 |
233
274
  | message | string | 返回描述 |
234
275
  | requestId | string | 后端请求 ID |
235
276
  | roomCode | string | 带看房间号 |
@@ -237,7 +278,7 @@ live.$RTC.on('userVolumes', (userVolumes: UserVolume[]) => {
237
278
 
238
279
  当 WebSocket 推送的数据结构体不满足 `LiveMsg` 或者 `code` 值不为 `SUCCESS` 时会以 `error` 事件的形式抛出来。
239
280
 
240
- - `error(error: Error): void` RTC 语音出现异常。
281
+ - `error(error: Error): void` - RTC 语音出现异常。
241
282
 
242
283
  > RTC 语音不同容器或 RTC 解决方案不同,其错误信息内容可能不完全一致。
243
284
 
@@ -245,7 +286,7 @@ live.$RTC.on('userVolumes', (userVolumes: UserVolume[]) => {
245
286
 
246
287
  语音能力需依赖 WebView/Browser 容器,即需要容器端实现 RTC 能力。
247
288
 
248
- 为了方便开发者接入,**如视开发者中心 VRTC 服务** 为主流平台提供 [容器 SDK](https://developers.realsee.com/docs/#/docs/live/client/container/README)
289
+ 为了方便开发者接入,**如视开放平台** 为主流平台提供 [容器 SDK](https://open-platform.realsee.com/developer/docs/live/intro/)
249
290
  和 [jsbridge-x](https://www.npmjs.com/package/@realsee/jsbridge-x) 。
250
291
 
251
292
  **Live** 通过 **jsbridge-x** 与 集成了**容器 SDK** 的客户端应用或微信小程序应用程序桥接,实现原生能力的调用。这个 `jsbridge-x`
@@ -253,30 +294,44 @@ live.$RTC.on('userVolumes', (userVolumes: UserVolume[]) => {
253
294
 
254
295
  如果是 iOS/Android App(已接入如视 VRTC 容器 SDK)
255
296
 
256
- ```js
297
+ ```typescript
257
298
  import JSBridge from '@realsee/jsbridge-x'
258
299
  import { createLive, VRWebViewRTC } from '@realsee/live'
259
300
 
260
301
  const jsBridgeInstance = new JSBridge()
261
302
 
262
303
  const rtcInstance = new VRWebViewRTC({
263
- jsBridge,
264
- getVoiceSign: () => {},
304
+ jsBridge: jsBridgeInstance,
305
+ getVoiceSign: async (params) => {
306
+ // 获取语音签名
307
+ const response = await fetch('/api/get-voice-sign', {
308
+ method: 'POST',
309
+ body: JSON.stringify(params)
310
+ })
311
+ return response.json()
312
+ },
265
313
  })
266
314
 
267
315
  const live = createLive({
268
316
  rtc: rtcInstance,
269
- getTicket: async () => '',
317
+ getTicket: async () => {
318
+ const response = await fetch('/api/get-ticket')
319
+ return response.json().ticket
320
+ },
270
321
  })
271
322
  ```
272
323
 
273
324
  提供 RTC 实例配置参数之后,语音的连接、断开、重连等逻辑就托管给了 **Live** 。您可以通过 live 实例上的 `$RTC` 命名空间访问语音相关的状态和事件。
274
325
 
275
- ```js
326
+ ```typescript
276
327
  // 状态:用户是否成功加入语音房间
277
328
  live.$RTC.joined
278
329
  // 状态:用户麦克风状态
279
330
  live.$RTC.micro
331
+ // 状态:当前语音房间ID
332
+ live.$RTC.voiceId
333
+ // 状态:RTC 类型
334
+ live.$RTC.type
280
335
  // 监听用户音量变化
281
336
  live.$RTC.on('userVolumes', (userVolumes) => {})
282
337
  ```
@@ -294,22 +349,33 @@ live.$RTC.on('userVolumes', (userVolumes) => {})
294
349
  ```shell
295
350
  # 需要您自行安装
296
351
  yarn add trtc-js-sdk
352
+ # 或
353
+ npm install trtc-js-sdk
297
354
  ```
298
355
 
299
356
  使用方式:
300
357
 
301
- ```tsx
358
+ ```typescript
302
359
  import { createLive, BrowserRTC } from '@realsee/live'
303
360
 
304
361
  const rtcInstance = new BrowserRTC({
305
- getVoiceSign: () => {},
362
+ getVoiceSign: async (params) => {
363
+ const response = await fetch('/api/get-voice-sign', {
364
+ method: 'POST',
365
+ body: JSON.stringify(params)
366
+ })
367
+ return response.json()
368
+ },
306
369
  // 可选:处理自动播放策略
307
370
  // autoPlayConfirm: ({ onConfirm }) => showDialogThen(onConfirm)
308
371
  })
309
372
 
310
373
  const live = createLive({
311
374
  rtc: rtcInstance,
312
- getTicket: async () => '',
375
+ getTicket: async () => {
376
+ const response = await fetch('/api/get-ticket')
377
+ return response.json().ticket
378
+ },
313
379
  })
314
380
  ```
315
381
 
@@ -320,20 +386,31 @@ const live = createLive({
320
386
  ```shell
321
387
  # 需要您自行安装
322
388
  yarn add amazon-chime-sdk-js
389
+ # 或
390
+ npm install amazon-chime-sdk-js
323
391
  ```
324
392
 
325
393
  使用方式:
326
394
 
327
- ```tsx
395
+ ```typescript
328
396
  import { createLive, BrowserRTC4Chime } from '@realsee/live'
329
397
 
330
398
  const rtcInstance = new BrowserRTC4Chime({
331
- getVoiceSign: () => {},
399
+ getVoiceSign: async (params) => {
400
+ const response = await fetch('/api/get-voice-sign', {
401
+ method: 'POST',
402
+ body: JSON.stringify(params)
403
+ })
404
+ return response.json()
405
+ },
332
406
  })
333
407
 
334
408
  const live = createLive({
335
409
  rtc: rtcInstance,
336
- getTicket: async () => '',
410
+ getTicket: async () => {
411
+ const response = await fetch('/api/get-ticket')
412
+ return response.json().ticket
413
+ },
337
414
  })
338
415
  ```
339
416
 
@@ -341,15 +418,25 @@ const live = createLive({
341
418
 
342
419
  使用方式:
343
420
 
344
- ```tsx
345
- import { createLive } from '@realsee/live'
421
+ ```typescript
422
+ import { createLive, BuiltInRTCType } from '@realsee/live'
346
423
 
347
424
  const live = createLive({
348
425
  useBuiltInRTC: true, // 使用内置rtc
349
426
  builtInRTCType: BuiltInRTCType.RealseeXChimeRTC, // 内置 rtc 类型,不填则默认trtc,chime请指明
350
427
  useBuiltInJsBridge: true, // 使用内置的jsBridge
351
- getVoiceSign, // 内置的rtc还是要把 getVoiceSign 方法传递进去
352
- getTicket: async () => '' /* 必须提供 */,
428
+ getVoiceSign: async (params) => {
429
+ // 内置的rtc还是要把 getVoiceSign 方法传递进去
430
+ const response = await fetch('/api/get-voice-sign', {
431
+ method: 'POST',
432
+ body: JSON.stringify(params)
433
+ })
434
+ return response.json()
435
+ },
436
+ getTicket: async () => {
437
+ const response = await fetch('/api/get-ticket')
438
+ return response.json().ticket
439
+ },
353
440
  })
354
441
 
355
442
  // 可以直接使用里面的 jsBridge, 该实例由 @realsee/jsbridge-x 提供支持
@@ -366,7 +453,7 @@ jsBridge.ready().then(() => console.info(`${jsBridge.webViewType} jsBridge is re
366
453
 
367
454
  步骤一:新建文件 `LiveReact.ts` 并创建您自己的 `LiveReact` 实例。
368
455
 
369
- ```tsx
456
+ ```typescript
370
457
  import { createLiveReact } from '@realsee/live'
371
458
 
372
459
  /** 定义帧数据快照结构 */
@@ -384,7 +471,18 @@ interface Snapshot {
384
471
  /** 通过 `createLiveReact()` 函数创建实例*/
385
472
 
386
473
  const LiveReact = createLiveReact<Snapshot>({
387
- getTicket: async () => '' /* 必须提供 */,
474
+ getTicket: async () => {
475
+ const response = await fetch('/api/get-ticket')
476
+ return response.json().ticket
477
+ },
478
+ getVoiceSign: async (params) => {
479
+ const response = await fetch('/api/get-voice-sign', {
480
+ method: 'POST',
481
+ body: JSON.stringify(params)
482
+ })
483
+ return response.json()
484
+ },
485
+ useBuiltInRTC: true,
388
486
  })
389
487
 
390
488
  export default LiveReact
@@ -393,31 +491,87 @@ export default LiveReact
393
491
  export const LiveProvider = LiveReact.LiveProvider
394
492
  export const useConnect = LiveReact.useConnect
395
493
  export const useKeyframe = LiveReact.useKeyframe
396
- // export ..
494
+ export const useLiveAction = LiveReact.useLiveAction
495
+ export const useLiveState = LiveReact.useLiveState
496
+ export const useUserList = LiveReact.useUserList
497
+ // export ...
397
498
  ```
398
499
 
399
500
  步骤二:通过 `LiveProvider` 将 `live` 实例集成到 `React` 的 [Context](https://reactjs.org/docs/context.html) 上下文环境中。
400
501
 
401
- ```jsx
502
+ ```tsx
402
503
  import { LiveProvider } from './LiveReact'
403
504
 
404
505
  ReactDOM.render(
405
506
  <LiveProvider>
406
507
  <RootApp />
407
508
  </LiveProvider>,
408
- containter,
509
+ container,
409
510
  )
410
511
  ```
411
512
 
412
513
  步骤三:在您的 `React` 函数组件中使用相关 `Hooks` 函数。
413
514
 
414
- ```jsx
415
- import { useConnect } from './LiveReact'
515
+ ### 可用的 React Hooks
516
+
517
+ **Live** 提供了丰富的 React Hooks,方便在 React 组件中使用:
518
+
519
+ - `useLiveInstance()` - 获取当前 Live 实例
520
+ - `useRTCInstance()` - 获取当前 RTC 实例
521
+ - `useConnect()` - 获取连接函数
522
+ - `useKeyframe(key)` - 获取指定键的帧数据状态和更新函数(类似 useState)
523
+ - `useKeyframeUpdateCallback(key, callback, deps)` - 监听指定键的帧数据更新回调
524
+ - `useLiveAction()` - 获取带看操作函数(connect, sendKeyframe, toggleMicro, broadcast, exit, kick, forbidMicro, forbidSync, getFramesByKey, setSelfInfo, updateStatus)
525
+ - `useLiveEventCallback(name, callback, deps)` - 监听带看事件
526
+ - `useLiveState()` - 获取带看连接状态
527
+ - `useRTCAction()` - 获取 RTC 操作函数(join, quit, toggleMicro, detectMicro, shock)
528
+ - `useRTCEventCallback(name, callback, deps)` - 监听 RTC 事件
529
+ - `useSelfInfo()` - 获取和更新用户信息(类似 useState)
530
+ - `useKeyframesSnapshot()` - 获取所有帧数据快照
531
+ - `useUserList()` - 获取用户列表
532
+
533
+ 使用示例:
534
+
535
+ ```tsx
536
+ import { useConnect, useLiveAction, useKeyframe, useLiveState, LiveState } from './LiveReact'
416
537
 
417
538
  function ReconnectBtn() {
418
539
  const connect = useConnect()
540
+ const liveState = useLiveState()
541
+
542
+ return (
543
+ <button
544
+ onClick={() => connect({
545
+ url: 'wss://example.com/live',
546
+ getTicket: async () => {
547
+ const response = await fetch('/api/get-ticket')
548
+ return response.json().ticket
549
+ }
550
+ })}
551
+ disabled={liveState === LiveState.CONNECTING || liveState === LiveState.OPEN}
552
+ >
553
+ {liveState === LiveState.OPEN ? '已连接' : '重新连接'}
554
+ </button>
555
+ )
556
+ }
557
+
558
+ function CameraControl() {
559
+ const [camera, setCamera] = useKeyframe('camera')
560
+ const { sendKeyframe } = useLiveAction()
561
+
562
+ const handleMove = () => {
563
+ const newCamera = { position: [1, 2, 3], rotation: [0, 0, 0] }
564
+ setCamera(newCamera) // 会自动同步到其他用户
565
+ // 或者使用 sendKeyframe
566
+ // sendKeyframe('camera', newCamera)
567
+ }
419
568
 
420
- return <button onClick={() => connect()}>重新连接</button>
569
+ return (
570
+ <div>
571
+ <p>相机位置: {camera?.position?.join(', ')}</p>
572
+ <button onClick={handleMove}>移动相机</button>
573
+ </div>
574
+ )
421
575
  }
422
576
  ```
423
577
 
@@ -429,5 +583,5 @@ function ReconnectBtn() {
429
583
  ## 相关文档链接
430
584
 
431
585
  - [@realsee/five](https://www.npmjs.com/package/@realsee/five)
432
- - [@realsee/jsbridge](https://www.npmjs.com/package/@realsee/jsbridge)
433
- - [如视开发者中心 VRTC 服务](https://developers.realsee.com/docs/#/docs/live/README)
586
+ - [@realsee/jsbridge-x](https://www.npmjs.com/package/@realsee/jsbridge-x)
587
+ - [如视开放平台线上带看](https://open-platform.realsee.com/developer/docs/live/intro/)