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