@yrzhao/aves-core 0.3.0 → 1.0.0

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.
Files changed (2) hide show
  1. package/README.md +58 -175
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -1,22 +1,8 @@
1
1
  # Aves Core
2
2
 
3
- 一个轻量级的 WebRTC 客户端库,用于实时点对点通信。提供消息、文件传输、语音、视频和屏幕共享能力,基于全网状拓扑的 DataChannel / MediaStream 直连。
3
+ 轻量级 WebRTC 客户端库,零运行时依赖。支持消息、文件传输、语音、视频、屏幕共享,基于全网状 P2P 拓扑直连。
4
4
 
5
- 当前版本:`0.3.0`
6
-
7
- ## 特性
8
-
9
- - 基于房间的通信模型,全网状 P2P 拓扑
10
- - 文本消息广播与单播(DataChannel 直传,不经服务器)
11
- - 文件传输:分块二进制传输,握手协议,进度追踪
12
- - 实时语音 / 视频:getUserMedia + RTP transceiver,静音和视频开关控制
13
- - 屏幕共享:getDisplayMedia,支持结束后恢复摄像头视频轨道
14
- - 自动断线重连与会话恢复
15
- - 内置 WebSocket 信令客户端,request/response 关联
16
- - 结构化 `AvesError`,错误码、阶段、重试语义和上下文可编程处理
17
- - 事件驱动架构,完整 TypeScript 类型定义
18
- - 零运行时依赖
19
- - CJS / ESM / TypeScript types 发布入口
5
+ 版本:`0.3.0`
20
6
 
21
7
  ## 安装
22
8
 
@@ -29,214 +15,111 @@ npm install @yrzhao/aves-core
29
15
  ```typescript
30
16
  import { AvesClient } from "@yrzhao/aves-core";
31
17
 
32
- const client = new AvesClient({
33
- signalingUrl: "ws://localhost:8080",
34
- });
18
+ const client = new AvesClient({ signalingUrl: "ws://localhost:8080" });
35
19
 
36
- // 监听事件
37
- client.on("message", (peerId, message) => {
38
- console.log(`来自 ${peerId}:`, message);
39
- });
20
+ client.on("message", (peerId, message) => console.log(`${peerId}:`, message));
21
+ client.on("userJoined", (p) => console.log(`${p.name} 加入了`));
40
22
 
41
- client.on("userJoined", (participant) => {
42
- console.log(`${participant.name} 加入了房间`);
43
- });
44
-
45
- // 创建并加入房间
46
23
  const roomId = await client.createRoom();
47
- const participants = await client.joinRoom(roomId, "Alice");
24
+ await client.joinRoom(roomId, "Alice");
48
25
 
49
- // 发送消息
50
26
  client.sendMessage({ text: "大家好!" });
51
-
52
- // 离开房间
53
27
  await client.leaveRoom();
54
28
  ```
55
29
 
56
- ## 文件传输
30
+ ## 核心 API
31
+
32
+ ### 房间管理
33
+
34
+ | 方法 | 返回 | 说明 |
35
+ |------|------|------|
36
+ | `createRoom()` | `Promise<string>` | 创建房间 |
37
+ | `joinRoom(roomId, name)` | `Promise<Participant[]>` | 加入房间 |
38
+ | `leaveRoom()` | `Promise<void>` | 离开 |
39
+ | `destroy()` | `void` | 销毁客户端 |
40
+
41
+ ### 消息
57
42
 
58
43
  ```typescript
59
- import type { FileTransferProgress, FileTransferResult } from "@yrzhao/aves-core";
44
+ client.sendMessage({ text: "hi" }); // 广播
45
+ client.sendMessageToPeer(peerId, { text: "hi" }); // 单播
46
+ ```
60
47
 
61
- // 发送文件(广播给所有对等端)
48
+ ### 文件传输
49
+
50
+ ```typescript
51
+ // 发送(广播)
62
52
  await client.sendFile(fileBlob, { fileName: "photo.jpg" });
63
53
 
64
- // 发送文件给特定对等端
65
- await client.sendFile(fileBlob, { fileName: "doc.pdf", peerId: "peer-123" });
54
+ // 发送(单播)
55
+ await client.sendFile(fileBlob, { fileName: "doc.pdf", peerId: targetId });
66
56
 
67
- // 监听传输进度
68
- client.on("fileTransferProgress", (peerId: string, progress: FileTransferProgress) => {
69
- console.log(`${peerId} ${progress.name}: ${Math.round(progress.progress * 100)}%`);
57
+ // 监听
58
+ client.on("fileTransferProgress", (p, pgr) => {
59
+ console.log(`${p}: ${Math.round(pgr.progress * 100)}%`);
70
60
  });
71
-
72
- // 接收文件
73
- client.on("fileTransferCompleted", (peerId: string, result: FileTransferResult) => {
74
- // result.blob: Blob, result.name: string
75
- if (!result.blob) return;
61
+ client.on("fileTransferCompleted", (_, result) => {
76
62
  const url = URL.createObjectURL(result.blob);
77
- // 用于下载或显示
78
63
  });
79
64
  ```
80
65
 
81
- ## 语音通话
66
+ ### 语音
82
67
 
83
68
  ```typescript
84
- // 开启语音(获取麦克风,发送给所有对等端)
85
69
  await client.startVoice();
86
-
87
- // 静音/取消静音
88
70
  client.setMuted(true);
89
-
90
- // 获取本地音频状态
91
- const state = client.getLocalAudioState();
92
- // { active: boolean, muted: boolean }
93
-
94
- // 获取远端音频流
95
- const remoteStream = client.getRemoteAudioStream("peer-123");
96
- if (remoteStream) {
97
- const audio = new Audio();
98
- audio.srcObject = remoteStream;
99
- audio.play();
100
- }
101
-
102
- // 关闭语音
103
71
  client.stopVoice();
104
72
  ```
105
73
 
106
- ## 视频与屏幕共享
74
+ ### 视频
107
75
 
108
76
  ```typescript
109
- // 开启摄像头视频
110
77
  await client.startVideo({ width: 1280, height: 720, frameRate: 30 });
111
-
112
- // 暂停/恢复本地视频轨道
113
78
  client.setVideoMuted(true);
114
-
115
- // 获取本地视频状态
116
- const videoState = client.getLocalVideoState();
117
- // { active: boolean, muted: boolean }
118
-
119
- // 获取远端视频流
120
- const remoteVideo = client.getRemoteVideoStream("peer-123");
121
-
122
- // 开始屏幕共享;如果摄像头已开启,停止共享后会恢复摄像头轨道
123
- await client.startScreenShare();
124
- client.stopScreenShare();
79
+ client.stopVideo();
125
80
  ```
126
81
 
127
- ## 配置
82
+ ### 屏幕共享
128
83
 
129
84
  ```typescript
130
- interface AvesClientConfig {
131
- signalingUrl: string; // 信令服务器 WebSocket URL(必需)
132
- iceServers?: RTCIceServer[]; // STUN/TURN 服务器
133
- fileChunkSize?: number; // 默认文件分块大小 bytes(默认:65536)
134
- video?: AvesVideoConstraints; // 默认视频约束
135
- reconnect?: {
136
- maxAttempts?: number; // 最大重连次数(默认:5)
137
- delay?: number; // 重连延迟 ms(默认:3000)
138
- requestTimeoutMs?: number; // 房间请求超时 ms(默认:30000)
139
- };
140
- debug?: boolean; // 调试日志(默认:false)
141
- }
85
+ await client.startScreenShare(); // 自动恢复摄像头
86
+ client.stopScreenShare();
142
87
  ```
143
88
 
144
- 默认值:
89
+ ## 配置
145
90
 
146
91
  ```typescript
147
- {
148
- iceServers: [{ urls: 'stun:stun.l.google.com:19302' }],
92
+ new AvesClient({
93
+ signalingUrl: "ws://localhost:8080", // 必需
94
+ iceServers: [{ urls: "stun:stun.l.google.com:19302" }],
149
95
  fileChunkSize: 65536,
150
96
  reconnect: { maxAttempts: 5, delay: 3000, requestTimeoutMs: 30000 },
151
- debug: false
152
- }
97
+ debug: false,
98
+ });
153
99
  ```
154
100
 
155
- ## API 参考
156
-
157
- ### 房间管理
158
-
159
- | 方法 | 返回值 | 说明 |
160
- |------|--------|------|
161
- | `createRoom()` | `Promise<string>` | 创建房间,返回 roomId |
162
- | `joinRoom(roomId, userName)` | `Promise<Participant[]>` | 加入房间 |
163
- | `joinRoom(roomId, userId, userName)` | `Promise<Participant[]>` | 以指定 userId 加入 |
164
- | `leaveRoom()` | `Promise<void>` | 离开房间,关闭所有连接 |
165
- | `destroy()` | `void` | 销毁客户端,清理全部资源 |
166
-
167
- ### 消息
168
-
169
- | 方法 | 说明 |
170
- |------|------|
171
- | `sendMessage(message)` | 广播消息给所有对等端 |
172
- | `sendMessageToPeer(peerId, message)` | 发送消息给特定对等端 |
173
-
174
- ### 文件与语音
175
-
176
- | 方法 | 说明 |
177
- |------|------|
178
- | `sendFile(blob, options?)` | 发送文件(`options.peerId` 存在时单播,否则广播) |
179
- | `startVoice()` | 开启语音 |
180
- | `stopVoice()` | 关闭语音 |
181
- | `setMuted(muted)` | 静音控制 |
182
- | `getLocalAudioState()` | 获取本地音频状态 |
183
- | `getRemoteAudioStream(peerId)` | 获取远端音频 MediaStream |
184
- | `startVideo(constraints?)` | 开启摄像头视频 |
185
- | `stopVideo()` | 关闭摄像头视频 |
186
- | `setVideoMuted(muted)` | 本地视频轨道开关 |
187
- | `getLocalVideoState()` | 获取本地视频状态 |
188
- | `getRemoteVideoStream(peerId)` | 获取远端视频 MediaStream |
189
- | `startScreenShare()` | 开始屏幕共享 |
190
- | `stopScreenShare()` | 停止屏幕共享 |
191
- | `getScreenShareState()` | 获取屏幕共享状态 |
192
-
193
- ### 状态查询
194
-
195
- | 方法 | 说明 |
196
- |------|------|
197
- | `getConnectionState(peerId)` | 对等连接状态 |
198
- | `getParticipants()` | 当前房间参与者列表 |
199
- | `isConnected()` | 信令连接是否活跃 |
200
-
201
101
  ## 事件
202
102
 
203
- | 事件 | 回调参数 | 说明 |
204
- |------|----------|------|
205
- | `message` | `(peerId, message)` | 收到对等端消息 |
206
- | `userJoined` | `(participant)` | 用户加入 |
207
- | `userLeft` | `(userId)` | 用户离开 |
208
- | `connectionStateChange` | `(peerId, state)` | 对等连接状态变化 |
209
- | `dataChannelStateChange` | `(peerId, state)` | 数据通道状态变化 |
210
- | `signalingStateChange` | `(state)` | 信令连接状态变化 |
211
- | `remoteAudioTrack` | `(peerId, stream, track)` | 收到远端音频流 |
212
- | `remoteVideoTrack` | `(peerId, stream, track)` | 收到远端视频流 |
213
- | `localAudioStateChange` | `(state)` | 本地音频状态变化 |
214
- | `localVideoStateChange` | `(state)` | 本地视频状态变化 |
215
- | `screenShareStateChange` | `(state)` | 屏幕共享状态变化 |
216
- | `fileTransferStarted` | `(peerId, info)` | 文件传输开始 |
217
- | `fileTransferProgress` | `(peerId, progress)` | 文件传输进度 |
218
- | `fileTransferCompleted` | `(peerId, result)` | 文件传输完成 |
219
- | `fileTransferFailed` | `(peerId, info, error)` | 文件传输失败 |
103
+ | 事件 | 参数 | 说明 |
104
+ |------|------|------|
105
+ | `message` | `(peerId, message)` | 收到消息 |
106
+ | `userJoined` / `userLeft` | `(user)` / `(userId)` | 用户进出 |
107
+ | `connectionStateChange` | `(peerId, state)` | 连接状态 |
108
+ | `localAudioStateChange` | `(state)` | 本地音频状态 |
109
+ | `localVideoStateChange` | `(state)` | 本地视频状态 |
110
+ | `screenShareStateChange` | `(state)` | 屏幕共享状态 |
111
+ | `fileTransferStarted/Progress/Completed/Failed` | — | 文件传输各阶段 |
112
+ | `remoteAudioTrack` / `remoteVideoTrack` | `(peerId, stream, track)` | 收到远端媒体 |
220
113
  | `error` | `(error)` | 错误 |
221
114
 
222
115
  ## 浏览器兼容性
223
116
 
224
- - Chrome/Edge 56+
225
- - Firefox 44+
226
- - Safari 11+
227
- - Opera 43+
228
-
229
- ## 发布与性能说明
117
+ Chrome/Edge 56+、Firefox 44+、Safari 11+、Opera 43+
230
118
 
231
- - 发布包仅包含 `dist`、`README.md`、`LICENSE` 和 `package.json`
232
- - 当前 dry-run tarball 约 30 KiB,解包后约 203 KiB
233
- - 主要运行时开销来自 WebRTC 浏览器实现、网络状况和房间内 peer 数量
234
- - 客户端全网状拓扑连接数为 O(n),每个 peer 会建立独立 RTCPeerConnection
119
+ ## 发布信息
235
120
 
236
- ## 许可证
121
+ - 包大小:~30 KiB gzip
122
+ - 全网状拓扑:O(n) 连接数
123
+ - 零运行时依赖
237
124
 
238
125
  MIT
239
-
240
- ## 相关包
241
-
242
- - [@yrzhao/aves-node](https://www.npmjs.com/package/@yrzhao/aves-node) - 信令服务器
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yrzhao/aves-core",
3
- "version": "0.3.0",
3
+ "version": "1.0.0",
4
4
  "description": "WebRTC client library for real-time communication",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/esm/index.js",