@relaxwork/verto 0.0.0-stage → 1.0.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/README.md +187 -2
- package/dist/base.d.ts +68 -0
- package/dist/index.d.ts +239 -0
- package/dist/index.js +663 -0
- package/dist/json-rpc.d.ts +157 -0
- package/dist/rtc.d.ts +88 -0
- package/package.json +40 -5
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import { VertoBase } from './base.js';
|
|
2
|
+
export interface JsonRpcClientParams {
|
|
3
|
+
/** FreeSWITCH Verto WebSocket/WSS 地址。 */
|
|
4
|
+
socketUrl: string;
|
|
5
|
+
/** 兼容旧版客户端的 HTTP 地址,目前不会被 JsonRpcClient 主动使用。 */
|
|
6
|
+
ajaxUrl?: string;
|
|
7
|
+
/** 登录用户名;启用自动认证重试时必填。 */
|
|
8
|
+
login?: string;
|
|
9
|
+
/** 登录密码;启用自动认证重试时必填。 */
|
|
10
|
+
passwd?: string;
|
|
11
|
+
/** 已有会话 ID,通常由服务端登录结果返回。 */
|
|
12
|
+
sessid?: string;
|
|
13
|
+
/** 发送给 login 方法的额外参数。 */
|
|
14
|
+
loginParams?: Record<string, unknown>;
|
|
15
|
+
/** 登录时传给 FreeSWITCH 的用户变量。 */
|
|
16
|
+
userVariables?: Record<string, unknown>;
|
|
17
|
+
/** 收到原始 WebSocket 消息时调用,适合调试或记录原始报文。 */
|
|
18
|
+
onmessage?: (event: MessageEvent<unknown>) => void;
|
|
19
|
+
/** 自定义 WebSocket 工厂;SSR、Node 或测试环境必须提供。 */
|
|
20
|
+
socketFactory?: WebSocketFactory;
|
|
21
|
+
/** 是否在连接断开后自动重连,默认 true。 */
|
|
22
|
+
reconnect?: boolean;
|
|
23
|
+
/** 首次重连等待时间,后续重连采用指数退避,单位为毫秒。 */
|
|
24
|
+
reconnectDelay?: number;
|
|
25
|
+
/** 最大重连次数;不设置时表示不限制次数。 */
|
|
26
|
+
maxReconnectAttempts?: number;
|
|
27
|
+
}
|
|
28
|
+
/** JsonRpcClient 所需的最小 WebSocket 接口,便于注入浏览器或 Node 实现。 */
|
|
29
|
+
export interface WebSocketLike {
|
|
30
|
+
/** WebSocket readyState,0 表示连接中,1 表示已打开,3 表示已关闭。 */
|
|
31
|
+
readonly readyState: number;
|
|
32
|
+
/** 连接成功后触发。 */
|
|
33
|
+
onopen: ((event: Event) => void) | null;
|
|
34
|
+
/** 收到服务端消息后触发。 */
|
|
35
|
+
onmessage: ((event: MessageEvent<unknown>) => void) | null;
|
|
36
|
+
/** 底层连接发生错误时触发。 */
|
|
37
|
+
onerror: ((event: Event) => void) | null;
|
|
38
|
+
/** 连接关闭后触发。 */
|
|
39
|
+
onclose: ((event: CloseEvent) => void) | null;
|
|
40
|
+
/** 发送一条文本 WebSocket 消息。 */
|
|
41
|
+
send(data: string): void;
|
|
42
|
+
/** 关闭 WebSocket 连接。 */
|
|
43
|
+
close(code?: number, reason?: string): void;
|
|
44
|
+
}
|
|
45
|
+
/** 创建 WebSocket 连接的工厂函数。 */
|
|
46
|
+
export type WebSocketFactory = (url: string) => WebSocketLike;
|
|
47
|
+
/** JSON-RPC 方法参数对象。 */
|
|
48
|
+
export type JsonRpcParams = Record<string, unknown>;
|
|
49
|
+
/** JSON-RPC 2.0 请求报文。 */
|
|
50
|
+
export interface JsonRpcRequest<TParams extends JsonRpcParams = JsonRpcParams> {
|
|
51
|
+
/** 请求 ID,用于匹配响应。 */
|
|
52
|
+
id: number;
|
|
53
|
+
/** 固定为 JSON-RPC 版本 2.0。 */
|
|
54
|
+
jsonrpc: '2.0';
|
|
55
|
+
/** FreeSWITCH 或 Verto 方法名。 */
|
|
56
|
+
method: string;
|
|
57
|
+
/** 方法参数。 */
|
|
58
|
+
params: TParams;
|
|
59
|
+
}
|
|
60
|
+
/** JSON-RPC 错误载荷。 */
|
|
61
|
+
export interface JsonRpcErrorPayload {
|
|
62
|
+
/** 服务端错误码。 */
|
|
63
|
+
code?: number;
|
|
64
|
+
/** 服务端错误消息。 */
|
|
65
|
+
message?: string;
|
|
66
|
+
/** 服务端附加错误数据。 */
|
|
67
|
+
data?: unknown;
|
|
68
|
+
}
|
|
69
|
+
/** JSON-RPC 调用失败时抛出的错误类型。 */
|
|
70
|
+
export declare class JsonRpcError extends Error {
|
|
71
|
+
/** FreeSWITCH 返回的错误码。 */
|
|
72
|
+
readonly code?: number;
|
|
73
|
+
/** FreeSWITCH 返回的附加错误数据。 */
|
|
74
|
+
readonly data?: unknown;
|
|
75
|
+
/**
|
|
76
|
+
* 创建 JSON-RPC 错误。
|
|
77
|
+
*
|
|
78
|
+
* @param payload 服务端返回的错误载荷。
|
|
79
|
+
*/
|
|
80
|
+
constructor(payload: JsonRpcErrorPayload);
|
|
81
|
+
}
|
|
82
|
+
/** Verto 信令层使用的 JSON-RPC 2.0 客户端。 */
|
|
83
|
+
export declare class JsonRpcClient extends VertoBase {
|
|
84
|
+
private readonly options;
|
|
85
|
+
private readonly pending;
|
|
86
|
+
private readonly queuedRequestIds;
|
|
87
|
+
private requestId;
|
|
88
|
+
private socket?;
|
|
89
|
+
private reconnectTimer?;
|
|
90
|
+
private reconnectAttempts;
|
|
91
|
+
private hasConnected;
|
|
92
|
+
private closed;
|
|
93
|
+
private eventHandler?;
|
|
94
|
+
private reconnectHandler?;
|
|
95
|
+
private readonly connectionWaiters;
|
|
96
|
+
/**
|
|
97
|
+
* 创建 JSON-RPC 客户端。
|
|
98
|
+
*
|
|
99
|
+
* @param options WebSocket、认证和重连配置。
|
|
100
|
+
* @param debug 是否输出发送报文和回调错误。
|
|
101
|
+
*/
|
|
102
|
+
constructor(options: JsonRpcClientParams, debug?: boolean);
|
|
103
|
+
/**
|
|
104
|
+
* 注册服务端通知处理器。
|
|
105
|
+
*
|
|
106
|
+
* 服务端推送的请求没有 result/error 响应,通常用于接收 verto.invite、
|
|
107
|
+
* verto.answer、verto.media 和 verto.bye。
|
|
108
|
+
*
|
|
109
|
+
* @param handler 收到通知时调用,参数依次为方法名和参数对象。
|
|
110
|
+
*/
|
|
111
|
+
setEventHandler(handler: (method: string, params: JsonRpcParams) => void): void;
|
|
112
|
+
/**
|
|
113
|
+
* 注册 WebSocket 重连回调。
|
|
114
|
+
*
|
|
115
|
+
* @param handler 连接在首次打开后再次建立时调用。
|
|
116
|
+
*/
|
|
117
|
+
setReconnectHandler(handler: () => void): void;
|
|
118
|
+
/**
|
|
119
|
+
* 主动打开 WebSocket 连接。
|
|
120
|
+
*
|
|
121
|
+
* 调用 call 时也会自动懒加载连接,因此大多数场景无需手动调用本方法。
|
|
122
|
+
*
|
|
123
|
+
* @returns WebSocket 完成 open 事件后的 Promise。
|
|
124
|
+
*/
|
|
125
|
+
connect(): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* 发送一个 JSON-RPC 2.0 请求。
|
|
128
|
+
*
|
|
129
|
+
* @typeParam TResult 服务端 result 的类型。
|
|
130
|
+
* @param method 方法名,例如 login 或 verto.info。
|
|
131
|
+
* @param params 方法参数对象,默认为空对象。
|
|
132
|
+
* @param success 兼容回调风格的成功回调,可选。
|
|
133
|
+
* @param failure 兼容回调风格的失败回调,可选。
|
|
134
|
+
* @returns 解析为服务端 result 的 Promise。
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* const result = await client.call<{ ok: boolean }>('ping', {});
|
|
138
|
+
*/
|
|
139
|
+
call<TResult = unknown>(method: string, params?: JsonRpcParams, success?: (data: TResult) => void, failure?: (error: JsonRpcError) => void): Promise<TResult>;
|
|
140
|
+
private createCall;
|
|
141
|
+
private ensureSocket;
|
|
142
|
+
private handleOpen;
|
|
143
|
+
private flushQueue;
|
|
144
|
+
private sendRequest;
|
|
145
|
+
private handleMessage;
|
|
146
|
+
private authenticateAndRetry;
|
|
147
|
+
private resolveRequest;
|
|
148
|
+
private rejectRequest;
|
|
149
|
+
private invoke;
|
|
150
|
+
private handleClose;
|
|
151
|
+
/**
|
|
152
|
+
* 关闭连接、取消自动重连并拒绝尚未完成的请求。
|
|
153
|
+
*
|
|
154
|
+
* 该方法不可逆;若要重新连接,请创建新的 JsonRpcClient 实例。
|
|
155
|
+
*/
|
|
156
|
+
close(): void;
|
|
157
|
+
}
|
package/dist/rtc.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { VertoBase } from './base.js';
|
|
2
|
+
/** 呼叫方向,用于决定是创建 offer 还是等待来电 offer。 */
|
|
3
|
+
export declare enum CallDirection {
|
|
4
|
+
/** 收到远端 offer 的来电。 */
|
|
5
|
+
Incoming = 0,
|
|
6
|
+
/** 由本地发起的去电。 */
|
|
7
|
+
Outgoing = 1
|
|
8
|
+
}
|
|
9
|
+
/** VertoRtc 发出的 WebRTC 生命周期事件。 */
|
|
10
|
+
export interface VertoRtcEvents {
|
|
11
|
+
/** ICE 收集完成后发送给 FreeSWITCH 的本地 offer。 */
|
|
12
|
+
'send-offer': RTCSessionDescriptionInit;
|
|
13
|
+
/** ICE 收集完成后发送给 FreeSWITCH 的本地 answer。 */
|
|
14
|
+
'send-answer': RTCSessionDescriptionInit;
|
|
15
|
+
/** 收到远端媒体轨道。 */
|
|
16
|
+
track: MediaStreamTrack;
|
|
17
|
+
/** WebRTC 协商失败或设置远端 SDP 失败。 */
|
|
18
|
+
error: Error;
|
|
19
|
+
}
|
|
20
|
+
/** VertoRtc 的可选配置。 */
|
|
21
|
+
export interface VertoRtcOptions {
|
|
22
|
+
/** 等待 ICE gathering 完成的最长时间,单位为毫秒。 */
|
|
23
|
+
iceTimeout?: number;
|
|
24
|
+
/** iceTimeout 的旧版拼写,供旧项目兼容。 */
|
|
25
|
+
ice_timeout?: number;
|
|
26
|
+
/** 自定义 RTCPeerConnection 工厂,适用于 Node、SSR 和测试环境。 */
|
|
27
|
+
peerConnectionFactory?: (configuration: RTCConfiguration) => RTCPeerConnection;
|
|
28
|
+
}
|
|
29
|
+
/** 管理一个 RTCPeerConnection,并将 SDP 协商转换为 Verto 事件。 */
|
|
30
|
+
export declare class VertoRtc extends VertoBase<VertoRtcEvents> {
|
|
31
|
+
private readonly peerConnection;
|
|
32
|
+
private readonly direction;
|
|
33
|
+
private readonly iceTimeout;
|
|
34
|
+
private state;
|
|
35
|
+
private pendingOfferSdp?;
|
|
36
|
+
private negotiationInProgress;
|
|
37
|
+
/**
|
|
38
|
+
* 创建一个 WebRTC 会话。
|
|
39
|
+
*
|
|
40
|
+
* @param configuration RTCPeerConnection 配置,例如 iceServers。
|
|
41
|
+
* @param direction 会话方向;去电会自动创建 offer,来电需要先调用 preSdp。
|
|
42
|
+
* @param iceTimeout ICE 收集超时时间,单位为毫秒。
|
|
43
|
+
* @param debug 是否输出 WebRTC 调试日志。
|
|
44
|
+
* @param peerConnectionFactory 可选的 RTCPeerConnection 工厂。
|
|
45
|
+
*/
|
|
46
|
+
constructor(configuration?: RTCConfiguration, direction?: CallDirection, iceTimeout?: number, debug?: boolean, peerConnectionFactory?: (configuration: RTCConfiguration) => RTCPeerConnection);
|
|
47
|
+
/** 获取底层 RTCPeerConnection,适合读取连接状态或调用 WebRTC 原生 API。 */
|
|
48
|
+
get connection(): RTCPeerConnection;
|
|
49
|
+
private onTrack;
|
|
50
|
+
private createOffer;
|
|
51
|
+
private dispatchLocalDescription;
|
|
52
|
+
private waitForIceGathering;
|
|
53
|
+
private emitError;
|
|
54
|
+
/** 获取底层 WebRTC 统计信息。 */
|
|
55
|
+
getStats(): Promise<RTCStatsReport>;
|
|
56
|
+
/**
|
|
57
|
+
* 设置远端 answer SDP。
|
|
58
|
+
*
|
|
59
|
+
* @param sdp FreeSWITCH 返回的 answer SDP 文本。
|
|
60
|
+
*/
|
|
61
|
+
onMedia(sdp: string): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* 暂存来电的 offer SDP,供后续 answer 使用。
|
|
64
|
+
*
|
|
65
|
+
* @param sdp FreeSWITCH 推送的 offer SDP 文本。
|
|
66
|
+
*/
|
|
67
|
+
preSdp(sdp: string): void;
|
|
68
|
+
/**
|
|
69
|
+
* 添加本地媒体轨道。
|
|
70
|
+
*
|
|
71
|
+
* @param track 由 getUserMedia 或其他 MediaStream 来源产生的轨道。
|
|
72
|
+
* @returns WebRTC 创建的 RTCRtpSender。
|
|
73
|
+
*/
|
|
74
|
+
addTrack(track: MediaStreamTrack): RTCRtpSender;
|
|
75
|
+
/**
|
|
76
|
+
* 为来电创建 answer,并在 ICE 收集完成后发出 send-answer 事件。
|
|
77
|
+
*
|
|
78
|
+
* @param tracks 接听时要发送给远端的本地媒体轨道。
|
|
79
|
+
* @throws 尚未调用 preSdp,或 WebRTC 协商失败时抛出错误。
|
|
80
|
+
*/
|
|
81
|
+
answer(tracks?: readonly MediaStreamTrack[]): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* 关闭 PeerConnection 并移除全部事件监听器。
|
|
84
|
+
*
|
|
85
|
+
* 关闭后不能继续使用当前 VertoRtc 实例进行协商。
|
|
86
|
+
*/
|
|
87
|
+
close(): void;
|
|
88
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@relaxwork/verto",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Framework-agnostic FreeSWITCH Verto JSON-RPC and WebRTC client",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"author": {
|
|
7
|
+
"name": "suzhenghui",
|
|
8
|
+
"email": "suzhenghui2003@qq.com"
|
|
9
|
+
},
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
17
|
+
"files": [
|
|
18
|
+
"dist"
|
|
19
|
+
],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "rslib",
|
|
22
|
+
"dev": "rslib --watch",
|
|
23
|
+
"lint": "rslint",
|
|
24
|
+
"test": "rstest",
|
|
25
|
+
"test:watch": "rstest --watch"
|
|
26
|
+
},
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@rslib/core": "^1.0.7",
|
|
29
|
+
"@rslint/core": "^0.9.5",
|
|
30
|
+
"@rstest/adapter-rslib": "^0.12.6",
|
|
31
|
+
"@rstest/core": "^0.12.6",
|
|
32
|
+
"@types/node": "^26.6.6",
|
|
33
|
+
"typescript": "^7.0.2"
|
|
34
|
+
},
|
|
35
|
+
"engines": {
|
|
36
|
+
"node": "^20.19.0 || >=22.12.0"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
}
|
|
41
|
+
}
|