@polyv/socket 2.0.0-rc-20250925.1

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/index.es.d.ts ADDED
@@ -0,0 +1,511 @@
1
+ import { AppSignSecretKey } from '@polyv/request-plugin-authorize-app-sign';
2
+ import { EventEmitter } from '@polyv/utils/es/event';
3
+ import { GetAppSignFn } from '@polyv/request-plugin-authorize-app-sign';
4
+ import { PickDomainInfo } from '@polyv/request-plugin-polyv-business';
5
+ import { PolyvRequest } from '@polyv/request-core';
6
+ import { RequestOptions } from '@polyv/request-core';
7
+ import { SignatureMethod } from '@polyv/request-plugin-authorize-app-sign';
8
+ import { SM2Key } from '@polyv/request-plugin-sm2';
9
+
10
+ /**
11
+ * 频道信息
12
+ */
13
+ export declare interface ChannelInfo {
14
+ /**
15
+ * 频道号
16
+ */
17
+ channelId: string;
18
+ /**
19
+ * 场次号
20
+ */
21
+ sessionId?: string;
22
+ /**
23
+ * 保利威账号 id
24
+ */
25
+ accountId?: string;
26
+ }
27
+
28
+ /**
29
+ * 频道令牌数据
30
+ */
31
+ export declare interface ChannelTokenData {
32
+ channelToken: string;
33
+ }
34
+
35
+ declare class ChatApiRequest extends PolyvRequest<ChatApiRequestOptions> {
36
+ private _plvSocket;
37
+ constructor(_plvSocket: PolyvSocket);
38
+ /**
39
+ * 获取聊天室签名
40
+ */
41
+ getChatSign(): Promise<string | undefined>;
42
+ }
43
+
44
+ declare interface ChatApiRequestOptions extends RequestOptions {
45
+ /**
46
+ * 插入频道号
47
+ * @default true
48
+ */
49
+ appendChannelId?: boolean;
50
+ /**
51
+ * 插入保利威账号 id
52
+ * @default false
53
+ */
54
+ appendAccountId?: boolean;
55
+ /**
56
+ * 插入用户 userId
57
+ * @default true
58
+ */
59
+ appendUserId?: boolean;
60
+ /**
61
+ * 插入频道号签名
62
+ * @default false
63
+ */
64
+ appendSign?: boolean;
65
+ }
66
+
67
+ /**
68
+ * socket 连接失败原因
69
+ */
70
+ export declare enum ConnectFailReason {
71
+ /**
72
+ * 令牌过期
73
+ */
74
+ TokenExpired = "TokenExpired",
75
+ /**
76
+ * socket 连接失败
77
+ */
78
+ SocketConnectError = "SocketConnectError",
79
+ /**
80
+ * socket 连接超时
81
+ */
82
+ SocketConnectTimeout = "SocketConnectTimeout"
83
+ }
84
+
85
+ /** 应用名 */
86
+ export declare type EventAppName = string;
87
+
88
+ /** 事件处理 */
89
+ export declare type EventHandler = (msg: any) => unknown;
90
+
91
+ /** 事件 key */
92
+ export declare type EventKey = string;
93
+
94
+ export declare type MaybePromise<R> = () => Promise<R> | R;
95
+
96
+ declare class PolyvApiRequest extends PolyvRequest<PolyvApiRequestOptions> {
97
+ constructor(_plvSocket: PolyvSocket);
98
+ }
99
+
100
+ declare interface PolyvApiRequestOptions extends RequestOptions {
101
+ }
102
+
103
+ export declare class PolyvSocket extends PolyvSocketBase {
104
+ ConnectFailReason: typeof ConnectFailReason;
105
+ SocketUserType: typeof SocketUserType;
106
+ /**
107
+ * 聊天室 api 请求器
108
+ */
109
+ chatApi: ChatApiRequest;
110
+ /**
111
+ * 保利威 api 请求器
112
+ */
113
+ polyvApi: PolyvApiRequest;
114
+ constructor(config: PolyvSocketConfig);
115
+ /**
116
+ * 设置 Socket 连接
117
+ */
118
+ setup(): Promise<void>;
119
+ protected _afterConnect(): void;
120
+ /**
121
+ * 登录聊天室 socket
122
+ */
123
+ loginSocket(): Promise<void>;
124
+ /**
125
+ * 判断是否为特殊的用户身份
126
+ *
127
+ * 用户模块提供 Api 用于判断用户身份是否为内置的特殊用户身份,如讲师、管理员、助教等。
128
+ * @param userType 用户身份
129
+ * @param actor 用户头衔
130
+ * @returns 是否特殊身份
131
+ */
132
+ isSpecialUserType(userType: SocketUserType | unknown, actor?: string): boolean;
133
+ /**
134
+ * 判断是否为当前用户
135
+ * @param userId 用户 id
136
+ */
137
+ isCurrentUser(userId: string): boolean;
138
+ }
139
+
140
+ /**
141
+ * Polyv Socket 基类
142
+ */
143
+ declare class PolyvSocketBase {
144
+ config: PolyvSocketConfig;
145
+ eventEmitter: EventEmitter<PolyvSocketEventRelations, PolyvSocketEvent>;
146
+ /**
147
+ * 事件存储
148
+ */
149
+ private __eventStore;
150
+ /**
151
+ * Socket 连接实例
152
+ */
153
+ private __socket?;
154
+ constructor(config: PolyvSocketConfig);
155
+ /**
156
+ * @hook 连接成功后
157
+ */
158
+ protected _afterConnect(): void;
159
+ /**
160
+ * 获取连接令牌
161
+ */
162
+ getToken(): Promise<string>;
163
+ /**
164
+ * 获取频道信息
165
+ */
166
+ getChannelInfo(): Promise<ChannelInfo>;
167
+ /**
168
+ * 获取用户信息
169
+ */
170
+ getUserInfo(): Promise<UserInfo>;
171
+ getSocket(check?: true): SocketInstance;
172
+ getSocket(check: false): SocketInstance | undefined;
173
+ /**
174
+ * 初始化 Socket
175
+ */
176
+ protected _initSocket(): Promise<void>;
177
+ /**
178
+ * 连接 Socket
179
+ */
180
+ protected _connectSocket(): Promise<void>;
181
+ /**
182
+ * 监听 socket 连接事件
183
+ */
184
+ private __bindConnectEvent;
185
+ /**
186
+ * 发送 socket 数据
187
+ * @param socketData socket 数据
188
+ * @param socketType socket 类型,默认:message
189
+ */
190
+ emitSocket<D = unknown>(socketData: unknown, socketType?: SocketEventType, options?: {
191
+ /** 是否直接回调参数,默认:false */
192
+ directCallback?: boolean;
193
+ /** 是否检查 code 返回,默认:true */
194
+ checkCode?: boolean;
195
+ }): Promise<D>;
196
+ /**
197
+ * 添加 socket 事件监听
198
+ * @param appName 应用名称
199
+ * @param handlers 消息事件处理对象
200
+ * @param socketType socket 事件类型
201
+ */
202
+ addSocketHandles(appName: EventAppName, handlers: SocketEventStoreHandlers | EventHandler, socketType?: SocketEventType): void;
203
+ /**
204
+ * 断开 Socket 连接
205
+ */
206
+ disconnect(): Promise<void>;
207
+ }
208
+
209
+ /**
210
+ * 保利威 socket 配置
211
+ */
212
+ export declare interface PolyvSocketConfig {
213
+ /**
214
+ * 连接令牌
215
+ */
216
+ token: string;
217
+ /**
218
+ * 频道信息
219
+ */
220
+ channelInfo: ChannelInfo;
221
+ /**
222
+ * 用户信息
223
+ */
224
+ userInfo: UserInfo;
225
+ /**
226
+ * 域名信息
227
+ */
228
+ domainInfo?: PickDomainInfo<'polyvApiDomain' | 'chatApiDomain' | 'chatWsDomain'>;
229
+ /**
230
+ * socket 连接版本号
231
+ *
232
+ * @default 3.0
233
+ */
234
+ socketVersion?: string;
235
+ /**
236
+ * 自定义 socket 连接实例,如果提供了该实例,则不会自动创建 socket 连接和连接 socket
237
+ */
238
+ customSocket?: SocketInstance;
239
+ /**
240
+ * 自动登录聊天室 socket
241
+ * @default true
242
+ */
243
+ autoLogin?: boolean;
244
+ /**
245
+ * 统计参数
246
+ */
247
+ statisticsParams?: StatisticsParams;
248
+ /**
249
+ * 获取用户令牌信息
250
+ */
251
+ getViewerToken?: MaybePromise<ViewerTokenData>;
252
+ /**
253
+ * 获取频道令牌信息
254
+ */
255
+ getChannelToken?: MaybePromise<ChannelTokenData>;
256
+ /**
257
+ * 授权方式
258
+ * @default RequestAuthorizeMethod.Token
259
+ */
260
+ authorizeMethod?: RequestAuthorizeMethod;
261
+ /**
262
+ * 获取 app 签名
263
+ */
264
+ getAppSign?: GetAppSignFn;
265
+ /**
266
+ * 应用签名密钥
267
+ */
268
+ appSignSecretKey?: AppSignSecretKey;
269
+ /**
270
+ * 是否使用放重放,仅 `RequestAuthorizeMethod.Sign` 时有效
271
+ * @default false
272
+ */
273
+ signatureNonce?: boolean;
274
+ /**
275
+ * 签名方式,仅 `RequestAuthorizeMethod.Sign` 时有效
276
+ * @default SignatureMethod.MD5
277
+ */
278
+ signatureMethod?: SignatureMethod;
279
+ /**
280
+ * 安全模式
281
+ */
282
+ securityMode?: SecurityMode;
283
+ /**
284
+ * SM2 密钥,仅 `SecurityMode.SM2` 时有效
285
+ */
286
+ sm2Key?: SM2Key;
287
+ }
288
+
289
+ export declare enum PolyvSocketEvent {
290
+ /**
291
+ * 连接成功
292
+ */
293
+ ConnectSuccess = "ConnectSuccess",
294
+ /**
295
+ * 重连成功
296
+ */
297
+ ReconnectSuccess = "ReconnectSuccess",
298
+ /**
299
+ * 连接失败
300
+ */
301
+ ConnectFailed = "ConnectFailed",
302
+ /**
303
+ * 断开连接
304
+ */
305
+ Disconnect = "Disconnect",
306
+ /**
307
+ * 登录成功
308
+ */
309
+ LoginSuccess = "LoginSuccess",
310
+ /**
311
+ * 登录失败
312
+ */
313
+ LoginFailed = "LoginFailed"
314
+ }
315
+
316
+ export declare type PolyvSocketEventRelations = {
317
+ [PolyvSocketEvent.ConnectFailed]: {
318
+ /**
319
+ * 连接失败原因
320
+ */
321
+ reason: ConnectFailReason;
322
+ };
323
+ [PolyvSocketEvent.LoginSuccess]: {
324
+ /**
325
+ * 登录状态码
326
+ */
327
+ code: number;
328
+ };
329
+ [PolyvSocketEvent.LoginFailed]: {
330
+ /**
331
+ * 登录状态码
332
+ */
333
+ code: number;
334
+ };
335
+ };
336
+
337
+ /**
338
+ * 请求授权方式
339
+ */
340
+ export declare enum RequestAuthorizeMethod {
341
+ /** 通过令牌授权 */
342
+ Token = "token",
343
+ /** 通过签名授权 */
344
+ Sign = "sign"
345
+ }
346
+
347
+ /**
348
+ * 安全模式
349
+ */
350
+ export declare enum SecurityMode {
351
+ /** 不使用 */
352
+ None = "none",
353
+ /** AES 加密解密 */
354
+ AES = "aes",
355
+ /** SM2 加密解密 */
356
+ SM2 = "sm2"
357
+ }
358
+
359
+ /**
360
+ * socket 的事件处理集合
361
+ */
362
+ export declare type SocketEventStoreHandlers = {
363
+ [eventKey: EventKey]: EventHandler;
364
+ };
365
+
366
+ /**
367
+ * socket 类型
368
+ */
369
+ export declare type SocketEventType = typeof socketEventTypes[number];
370
+
371
+ export declare const socketEventTypes: readonly ["message", "product", "lottery", "questionnaire", "coupon", "activityLottery", "testQuestion", "emotion", "speak", "customMessage", "motivation"];
372
+
373
+ export declare interface SocketInstance {
374
+ /** 是否已断开连接 */
375
+ disconnected: boolean;
376
+ /** 是否已连接 */
377
+ connected: boolean;
378
+ /** 连接 */
379
+ connect: () => void;
380
+ /** 断开连接 */
381
+ disconnect: () => void;
382
+ /** 发送消息 */
383
+ emit: (event: string, ...args: any[]) => void;
384
+ /** 监听事件 */
385
+ on: (event: string, callback: (...args: any[]) => void) => void;
386
+ /** 监听一次事件 */
387
+ once: (event: string, callback: (...args: any[]) => void) => void;
388
+ }
389
+
390
+ /**
391
+ * 用户身份类型枚举
392
+ */
393
+ export declare enum SocketUserType {
394
+ /**
395
+ * 普通频道观众身份
396
+ */
397
+ Student = "student",
398
+ /**
399
+ * 云课堂频道观众身份
400
+ */
401
+ Slice = "slice",
402
+ /**
403
+ * 观众
404
+ */
405
+ Viewer = "viewer",
406
+ /**
407
+ * 讲师
408
+ */
409
+ Teacher = "teacher",
410
+ /**
411
+ * 助教
412
+ */
413
+ Assistant = "assistant",
414
+ /**
415
+ * 管理员
416
+ */
417
+ Manager = "manager",
418
+ /**
419
+ * 嘉宾
420
+ */
421
+ Guest = "guest",
422
+ /**
423
+ * 标签管理员
424
+ */
425
+ UserTagsMonitor = "userTagsMonitor",
426
+ /**
427
+ * 研讨会参与者
428
+ */
429
+ Attendee = "attendee",
430
+ /**
431
+ * 研讨会观众身份
432
+ */
433
+ Listener = "listener",
434
+ /**
435
+ * 研讨会主持人
436
+ */
437
+ Host = "host",
438
+ /**
439
+ * 虚拟人
440
+ */
441
+ Dummy = "dummy"
442
+ }
443
+
444
+ /**
445
+ * 统计参数
446
+ */
447
+ export declare interface StatisticsParams {
448
+ /**
449
+ * 自定义统计参数 p4
450
+ */
451
+ param4?: string;
452
+ /**
453
+ * 自定义统计参数 p5
454
+ */
455
+ param5?: string;
456
+ }
457
+
458
+ /**
459
+ * 用户信息
460
+ */
461
+ export declare interface UserInfo {
462
+ /**
463
+ * 用户 id
464
+ */
465
+ userId: string;
466
+ /**
467
+ * 用户系统唯一 id
468
+ */
469
+ unionId?: string;
470
+ /**
471
+ * 用户昵称
472
+ */
473
+ nick: string;
474
+ /**
475
+ * 用户头像
476
+ */
477
+ pic: string;
478
+ /**
479
+ * 用户类型
480
+ */
481
+ userType: SocketUserType;
482
+ /**
483
+ * 头衔
484
+ */
485
+ actor?: string;
486
+ /**
487
+ * 微信 unionId
488
+ */
489
+ wxUnionId?: string;
490
+ /**
491
+ * 微信 openId
492
+ */
493
+ wxOpenId?: string;
494
+ /**
495
+ * 用户系统的标签 id
496
+ */
497
+ labelIds?: number[];
498
+ /**
499
+ * 企微用户标签id
500
+ */
501
+ workWechatTagId?: string;
502
+ }
503
+
504
+ /**
505
+ * 用户令牌数据
506
+ */
507
+ export declare interface ViewerTokenData {
508
+ viewerToken: string;
509
+ }
510
+
511
+ export { }