@cg-devcenter/rtc-fpnn-webjs-sdk 1.0.0-cg.3 → 1.0.0-cg.4
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 +23 -3
- package/dist/rtm.d.ts +42 -8
- package/dist/rtm.min.js +6 -6
- package/dist/rtm.shared-worker.js +4 -4
- package/dist/rtm.vite.d.ts +1 -0
- package/dist/rtm.vite.mjs +13 -2
- package/docs/CAPABILITIES.md +11 -5
- package/docs/PROTOCOL_MIGRATION.md +146 -0
- package/docs/USAGE.md +106 -6
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -20,6 +20,23 @@ Node.js 使用 TLV v1 时,需自行提供 `webSocketFactory(url)`。SDK 不携
|
|
|
20
20
|
|
|
21
21
|
## Quick Start
|
|
22
22
|
|
|
23
|
+
### 默认共享入口(当前源码)
|
|
24
|
+
|
|
25
|
+
`createClient()` 默认共享连接,默认协议为 TLV。Worker 名称由 SDK 按 **PID + 协议 + UID** 自动生成,UID 可直接从 `login()` 获取,业务无需维护 `workerName`。已发布 `.3` 不包含这个入口;请先使用本地构建或后续版本。
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
const client = rtm.createClient({ pid, tlvEndpoint: endpoint });
|
|
29
|
+
client.processor.on(rtm.RTMConfig.TLV_SERVER_PUSH.recvMessage, renderMessage);
|
|
30
|
+
await client.promises.login(uid, credential); // 同 UID 已登录时 credential 可省略
|
|
31
|
+
await client.promises.sendChat(peerUid, 'hello', '{}');
|
|
32
|
+
// 离开页面只释放本页,其他页面继续收发。
|
|
33
|
+
client.destroy();
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
片段中的 `rtm`、项目参数、credential 和 `renderMessage` 由应用提供;完整流程和能力边界见 [默认共享接入](docs/USAGE.md#默认共享接入当前源码)。设置 `sharing: false` 创建独立连接;旧 `new RTMClient()` 的默认行为保持不变。Vite 项目从包的 `/vite` 入口导入,SDK 自动打包 Worker。
|
|
37
|
+
|
|
38
|
+
### 原有单连接入口
|
|
39
|
+
|
|
23
40
|
从业务服务端取得当前 UID 的 credential,然后连接 Gate。credential 必须由服务端签发;项目 SecretKey 不要放进浏览器。
|
|
24
41
|
|
|
25
42
|
```js
|
|
@@ -53,9 +70,11 @@ async function startChat(rtm, { pid, endpoint, uid, peerUid, credential }) {
|
|
|
53
70
|
|
|
54
71
|
## 同 UID 多窗口
|
|
55
72
|
|
|
56
|
-
需要同一浏览器内多个同源页面共用一个 UID 的 RTC 登录和 WebSocket
|
|
73
|
+
需要同一浏览器内多个同源页面共用一个 UID 的 RTC 登录和 WebSocket 时,新接入推荐使用 SDK 的 `createClient()` 默认共享入口。SDK 按 PID、协议和 UID 确定共享会话,业务代码无需维护 `workerName`;普通浏览器部署时将 `rtm.min.js` 和 `rtm.shared-worker.js` 放在同一同源目录即可。首次登录需要 credential;同 UID 后续页面复用共享会话。完整说明见 [默认共享接入](docs/USAGE.md#默认共享接入当前源码)。
|
|
74
|
+
|
|
75
|
+
Vite 项目从 `@cg-devcenter/rtc-fpnn-webjs-sdk/vite` 导入 ESM 入口,SDK 会自动打包和配置 Worker 文件。若使用底层 `RTMSharedWorkerClient` 构造器,可通过 `uid` 自动生成 Worker 名称,或显式传入 `workerName`;`createClient()` 会按登录 UID 自动绑定,不要求手动设置名称。最后一个页面 detach 后默认 5 秒关闭共享连接。需要固定在已发布 `.3` 上时仍可直接使用 `RTMSharedWorkerClient`;当前源码新增的 `createClient()` 尚未包含在 `.3` 中。
|
|
57
76
|
|
|
58
|
-
|
|
77
|
+
当前源码的 SharedWorker 也支持显式选择 `clientOptions.protocol: 'fpnn'`,登录共享和会话路由沿用同一套 API。已发布的 `1.0.0-cg.3` 尚不含 FPNN 共享扩展;本地构建或后续版本的用法与限制见 [FPNN 多窗口接入](docs/USAGE.md#fpnn-多窗口接入)。
|
|
59
78
|
|
|
60
79
|
仓库的演示页面可用以下命令启动:
|
|
61
80
|
|
|
@@ -66,7 +85,7 @@ npm run demo:shared-worker
|
|
|
66
85
|
|
|
67
86
|
发送台和收件箱 demo 地址、真实 Gate 配置方式见 [Demo 使用说明](examples/shared-worker-demo/README.md)。
|
|
68
87
|
|
|
69
|
-
另有使用 Vue 3、Vite 和 npm
|
|
88
|
+
另有使用 Vue 3、Vite 和 npm 包导入方式的同功能示例;本次新入口需先通过 yalc 链接本地构建:[Vue 3 SharedWorker Demo](examples/vue3-shared-worker-demo/README.md)。
|
|
70
89
|
|
|
71
90
|
## TLV v1 最小接入 Demo
|
|
72
91
|
|
|
@@ -75,6 +94,7 @@ npm run demo:shared-worker
|
|
|
75
94
|
## 文档
|
|
76
95
|
|
|
77
96
|
- [接入指南与 API 参数](docs/USAGE.md)
|
|
97
|
+
- [FPNN 与 TLV v1 迁移对照](docs/PROTOCOL_MIGRATION.md)
|
|
78
98
|
- [能力总览与典型业务流程](docs/CAPABILITIES.md)
|
|
79
99
|
- [TLV v1 URI、请求字段与返回值](docs/TLV_CONTRACT.md)
|
|
80
100
|
- [TLV v1 传输规则](docs/TLV.md)
|
package/dist/rtm.d.ts
CHANGED
|
@@ -31,6 +31,11 @@ declare namespace rtm {
|
|
|
31
31
|
|
|
32
32
|
export type RTMSharedWorkerPromiseApi = RTMPromiseApi;
|
|
33
33
|
|
|
34
|
+
export type RTMDefaultPromiseApi = Omit<RTMPromiseApi, 'login'> & {
|
|
35
|
+
/** Only the first same-UID page requires a credential. */
|
|
36
|
+
login(uid: RTMId, credential?: string, timeout?: number): Promise<RTMLoginPromiseResult>;
|
|
37
|
+
};
|
|
38
|
+
|
|
34
39
|
interface RTMRegressiveStrategy {
|
|
35
40
|
startConnectFailedCount: number;
|
|
36
41
|
maxIntervalSeconds: number;
|
|
@@ -188,12 +193,9 @@ declare namespace rtm {
|
|
|
188
193
|
removeEvent();
|
|
189
194
|
}
|
|
190
195
|
|
|
191
|
-
export interface
|
|
196
|
+
export interface RTMSharedWorkerBaseClientOptions {
|
|
192
197
|
pid: number;
|
|
193
|
-
|
|
194
|
-
protocol?: 'tlv-v1';
|
|
195
|
-
/** Required full ws:// or wss:// WebSocket URL. */
|
|
196
|
-
tlvEndpoint: string;
|
|
198
|
+
maxPingIntervalSeconds?: number;
|
|
197
199
|
autoReconnect?: boolean;
|
|
198
200
|
connectionTimeout?: number;
|
|
199
201
|
requestTimeout?: number;
|
|
@@ -210,6 +212,14 @@ declare namespace rtm {
|
|
|
210
212
|
regressiveStrategy?: RTMRegressiveStrategy;
|
|
211
213
|
}
|
|
212
214
|
|
|
215
|
+
/** Defaults to TLV; FPNN must be explicitly selected. Functions cannot cross the Worker boundary. */
|
|
216
|
+
export type RTMSharedWorkerClientOptions = RTMSharedWorkerBaseClientOptions & (
|
|
217
|
+
{ protocol?: 'tlv-v1'; tlvEndpoint: string }
|
|
218
|
+
| ({ protocol: 'fpnn' } & (
|
|
219
|
+
{ endpoint: string; ssl_endpoint?: string } | { ssl_endpoint: string; endpoint?: string }
|
|
220
|
+
))
|
|
221
|
+
);
|
|
222
|
+
|
|
213
223
|
export type RTMConversationType = 'direct' | 'group' | 'room';
|
|
214
224
|
|
|
215
225
|
export interface RTMConversation {
|
|
@@ -221,7 +231,7 @@ declare namespace rtm {
|
|
|
221
231
|
export interface RTMSharedWorkerOptions {
|
|
222
232
|
/** Optional same-origin Worker URL. The classic entry defaults beside its script; the Vite entry bundles this resource automatically. */
|
|
223
233
|
workerUrl?: string;
|
|
224
|
-
/** Explicit stable name. When omitted and uid is provided, the SDK derives one from pid, uid, endpoint, and workerNamespace. */
|
|
234
|
+
/** Explicit stable name. When omitted and uid is provided, the SDK derives one from protocol, pid, uid, endpoint, and workerNamespace. */
|
|
225
235
|
workerName?: string;
|
|
226
236
|
/** UID used only to derive workerName when workerName is omitted; it does not log in automatically. */
|
|
227
237
|
uid?: RTMId;
|
|
@@ -231,12 +241,34 @@ declare namespace rtm {
|
|
|
231
241
|
readyTimeout?: number;
|
|
232
242
|
/** Close and release the RTC session after the last page detaches; defaults to 5000 ms. Use 0 to close immediately. All tabs sharing a Worker must use the same value. */
|
|
233
243
|
idleShutdownTimeout?: number;
|
|
234
|
-
/** Structured-cloneable options
|
|
244
|
+
/** Structured-cloneable RTM options. Supports TLV and FPNN; file uploads remain unsupported. */
|
|
235
245
|
clientOptions: RTMSharedWorkerClientOptions;
|
|
236
246
|
/** Optional route for this page. When omitted, this page receives all conversation Push events. */
|
|
237
247
|
conversation?: RTMConversation | null;
|
|
238
248
|
}
|
|
239
249
|
|
|
250
|
+
/** Flat client options. Browser sharing defaults to true; protocol defaults to TLV in both modes. */
|
|
251
|
+
export type RTMCreateClientOptions = RTMSharedWorkerClientOptions &
|
|
252
|
+
Omit<RTMSharedWorkerOptions, 'clientOptions'> & { sharing?: true };
|
|
253
|
+
|
|
254
|
+
/** Default shared facade. Worker is attached on login, or immediately when options.uid is set. */
|
|
255
|
+
export interface RTMDefaultClient extends Omit<RTMSharedWorkerClient, 'promises' | 'login' | 'whenReady'> {
|
|
256
|
+
readonly promises: RTMDefaultPromiseApi;
|
|
257
|
+
on(type: 'SharedWorkerReady' | 'SharedSessionState', callback: (session: RTMSharedSessionSnapshot) => void): this;
|
|
258
|
+
on(type: string, callback: (...args: any[]) => void): this;
|
|
259
|
+
off(type: string, callback?: (...args: any[]) => void): this;
|
|
260
|
+
removeEvent(type?: string, callback?: (...args: any[]) => void): this;
|
|
261
|
+
setConversation(conversation: RTMConversation | null, callback?: (error?: Error | null) => void): this;
|
|
262
|
+
/** All callback failures use (false, numericCode); local/Worker details are emitted as SharedWorkerError. */
|
|
263
|
+
login(uid: RTMId, credential?: string, callback?: (accepted: boolean, errorCode?: number) => void, timeout?: number): void;
|
|
264
|
+
/** Requires a UID from options.uid, login(), or this argument. Never waits for an unspecified UID. */
|
|
265
|
+
whenReady(uid?: RTMId): Promise<RTMSharedSessionSnapshot>;
|
|
266
|
+
emit(type: string, ...args: any[]): void;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
export function createClient(options: RTMClientOptions & { sharing: false }): RTMClient;
|
|
270
|
+
export function createClient(options: RTMCreateClientOptions): RTMDefaultClient;
|
|
271
|
+
|
|
240
272
|
export interface RTMSharedSessionSnapshot {
|
|
241
273
|
status: 'idle' | 'connecting' | 'authenticated';
|
|
242
274
|
/** Decimal UID string for the shared session; null while idle. */
|
|
@@ -260,7 +292,7 @@ declare namespace rtm {
|
|
|
260
292
|
|
|
261
293
|
export class RTMSharedWorkerClient {
|
|
262
294
|
constructor(options: RTMSharedWorkerOptions);
|
|
263
|
-
/** Creates a stable name for the same project, UID, endpoint, and optional application/environment namespace. */
|
|
295
|
+
/** Creates a stable name for the same protocol, project, UID, endpoint, and optional application/environment namespace. */
|
|
264
296
|
static createWorkerName(identity: RTMSharedWorkerIdentity): string;
|
|
265
297
|
/** Promise facade for callback-based business request methods. */
|
|
266
298
|
readonly promises: RTMSharedWorkerPromiseApi;
|
|
@@ -283,6 +315,8 @@ declare namespace rtm {
|
|
|
283
315
|
}
|
|
284
316
|
|
|
285
317
|
export interface RTMSharedWorkerIdentity {
|
|
318
|
+
/** Defaults to tlv-v1. FPNN names are isolated from TLV names. */
|
|
319
|
+
protocol?: RTMProtocol;
|
|
286
320
|
pid: string | number;
|
|
287
321
|
uid: RTMId;
|
|
288
322
|
endpoint?: string;
|