@quitting/core 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.
- package/dist/api-error.d.ts +8 -0
- package/dist/api-error.d.ts.map +1 -0
- package/dist/atim-core.es.js +1072 -0
- package/dist/atim-core.es.js.map +1 -0
- package/dist/atim-core.umd.js +2 -0
- package/dist/atim-core.umd.js.map +1 -0
- package/dist/auth.d.ts +25 -0
- package/dist/auth.d.ts.map +1 -0
- package/dist/call.d.ts +58 -0
- package/dist/call.d.ts.map +1 -0
- package/dist/events.d.ts +9 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/index.d.ts +85 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/media.d.ts +10 -0
- package/dist/media.d.ts.map +1 -0
- package/dist/meeting.d.ts +83 -0
- package/dist/meeting.d.ts.map +1 -0
- package/dist/message.d.ts +37 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/realtime.d.ts +21 -0
- package/dist/realtime.d.ts.map +1 -0
- package/dist/transport.d.ts +83 -0
- package/dist/transport.d.ts.map +1 -0
- package/package.json +47 -0
|
@@ -0,0 +1,1072 @@
|
|
|
1
|
+
import { Centrifuge as e } from "centrifuge";
|
|
2
|
+
import { Room as t, Track as n } from "livekit-client";
|
|
3
|
+
//#region src/events.ts
|
|
4
|
+
var r = class {
|
|
5
|
+
listeners = /* @__PURE__ */ new Map();
|
|
6
|
+
on(e, t) {
|
|
7
|
+
return this.listeners.has(e) || this.listeners.set(e, /* @__PURE__ */ new Set()), this.listeners.get(e).add(t), () => this.off(e, t);
|
|
8
|
+
}
|
|
9
|
+
off(e, t) {
|
|
10
|
+
this.listeners.get(e)?.delete(t);
|
|
11
|
+
}
|
|
12
|
+
emit(e, t) {
|
|
13
|
+
let n = this.listeners.get(e);
|
|
14
|
+
if (n) for (let r of n) try {
|
|
15
|
+
r(t);
|
|
16
|
+
} catch (t) {
|
|
17
|
+
console.error(`[atim] event handler error: ${e}`, t);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
removeAllListeners() {
|
|
21
|
+
this.listeners.clear();
|
|
22
|
+
}
|
|
23
|
+
}, i = class extends Error {
|
|
24
|
+
code;
|
|
25
|
+
httpStatus;
|
|
26
|
+
constructor(e, t, n) {
|
|
27
|
+
super(n), this.name = "ApiError", this.code = e, this.httpStatus = t;
|
|
28
|
+
}
|
|
29
|
+
}, a = class {
|
|
30
|
+
apiBase;
|
|
31
|
+
tokenGetter;
|
|
32
|
+
constructor(e, t) {
|
|
33
|
+
this.apiBase = e.replace(/\/$/, ""), this.tokenGetter = t;
|
|
34
|
+
}
|
|
35
|
+
async request(e, t = {}) {
|
|
36
|
+
let n = `${this.apiBase}${e}`, r = {
|
|
37
|
+
"Content-Type": "application/json",
|
|
38
|
+
...t.headers
|
|
39
|
+
}, a = this.tokenGetter();
|
|
40
|
+
a && (r.Authorization = `Bearer ${a}`), r["X-Trace-Id"] = crypto.randomUUID?.() || Math.random().toString(36).slice(2);
|
|
41
|
+
let o = await fetch(n, {
|
|
42
|
+
...t,
|
|
43
|
+
headers: r
|
|
44
|
+
});
|
|
45
|
+
if (!o.ok) {
|
|
46
|
+
let e = 0, t = `HTTP ${o.status}`;
|
|
47
|
+
try {
|
|
48
|
+
let n = await o.json();
|
|
49
|
+
n?.code && (e = Number(n.code)), n?.message && (t = n.message);
|
|
50
|
+
} catch {}
|
|
51
|
+
throw new i(e, o.status, t);
|
|
52
|
+
}
|
|
53
|
+
return o.json();
|
|
54
|
+
}
|
|
55
|
+
async sendBeacon(e, t) {
|
|
56
|
+
let n = `${this.apiBase}${e}`, r = { "Content-Type": "application/json" }, i = this.tokenGetter();
|
|
57
|
+
i && (r.Authorization = `Bearer ${i}`);
|
|
58
|
+
try {
|
|
59
|
+
await fetch(n, {
|
|
60
|
+
method: "POST",
|
|
61
|
+
headers: r,
|
|
62
|
+
body: JSON.stringify(t),
|
|
63
|
+
keepalive: !0
|
|
64
|
+
});
|
|
65
|
+
} catch (e) {
|
|
66
|
+
console.warn("[atim] sendBeacon 失败(页面卸载中可能正常)", e);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
getClientConfig() {
|
|
70
|
+
return this.request("/client/config");
|
|
71
|
+
}
|
|
72
|
+
login(e, t, n = 4, r) {
|
|
73
|
+
return this.request("/auth/login", {
|
|
74
|
+
method: "POST",
|
|
75
|
+
body: JSON.stringify({
|
|
76
|
+
appId: e,
|
|
77
|
+
externalId: t,
|
|
78
|
+
clientType: n,
|
|
79
|
+
deviceName: r
|
|
80
|
+
})
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
refresh() {
|
|
84
|
+
return this.request("/auth/refresh", { method: "POST" });
|
|
85
|
+
}
|
|
86
|
+
getCentrifugoToken(e, t) {
|
|
87
|
+
let n = new URLSearchParams();
|
|
88
|
+
return e && n.set("name", e), t && n.set("avatar", t), this.request(`/auth/centrifugo-token?${n.toString()}`);
|
|
89
|
+
}
|
|
90
|
+
getSessionState(e) {
|
|
91
|
+
let t = e ? `?uid=${encodeURIComponent(e)}` : "";
|
|
92
|
+
return this.request(`/session/state${t}`);
|
|
93
|
+
}
|
|
94
|
+
usersByExternal(e) {
|
|
95
|
+
return this.request(`/users/by-external?ids=${e.map(encodeURIComponent).join(",")}`);
|
|
96
|
+
}
|
|
97
|
+
callInit(e, t) {
|
|
98
|
+
return this.request("/call/init", {
|
|
99
|
+
method: "POST",
|
|
100
|
+
body: JSON.stringify({
|
|
101
|
+
calleeUid: e,
|
|
102
|
+
type: t
|
|
103
|
+
})
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
callAccept(e) {
|
|
107
|
+
return this.request("/call/accept", {
|
|
108
|
+
method: "POST",
|
|
109
|
+
body: JSON.stringify({ callId: e })
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
callEnd(e, t) {
|
|
113
|
+
return this.request("/call/end", {
|
|
114
|
+
method: "POST",
|
|
115
|
+
body: JSON.stringify({
|
|
116
|
+
callId: e,
|
|
117
|
+
reason: t
|
|
118
|
+
})
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
meetingCreate(e) {
|
|
122
|
+
return this.request("/meeting/create", {
|
|
123
|
+
method: "POST",
|
|
124
|
+
body: JSON.stringify(e)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
meetingJoin(e) {
|
|
128
|
+
return this.request("/meeting/join", {
|
|
129
|
+
method: "POST",
|
|
130
|
+
body: JSON.stringify({ meetingId: e })
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
meetingRespond(e, t) {
|
|
134
|
+
return this.request(`/meeting/${e}/respond`, {
|
|
135
|
+
method: "POST",
|
|
136
|
+
body: JSON.stringify({ action: t })
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
meetingKick(e, t) {
|
|
140
|
+
return this.request(`/meeting/${e}/kick`, {
|
|
141
|
+
method: "POST",
|
|
142
|
+
body: JSON.stringify({ uid: t })
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
meetingReinvite(e, t) {
|
|
146
|
+
return this.request(`/meeting/${e}/reinvite`, {
|
|
147
|
+
method: "POST",
|
|
148
|
+
body: JSON.stringify({ uid: t })
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
meetingLeave(e) {
|
|
152
|
+
return this.request(`/meeting/${e}/leave`, { method: "POST" });
|
|
153
|
+
}
|
|
154
|
+
meetingDissolve(e) {
|
|
155
|
+
return this.request(`/meeting/${e}/dissolve`, { method: "POST" });
|
|
156
|
+
}
|
|
157
|
+
meetingParticipantCount(e) {
|
|
158
|
+
return this.request(`/meeting/${e}/participant-count`);
|
|
159
|
+
}
|
|
160
|
+
meetingPendingInvites() {
|
|
161
|
+
return this.request("/meetings/pending-invites");
|
|
162
|
+
}
|
|
163
|
+
meetingMembers(e) {
|
|
164
|
+
return this.request(`/meeting/${e}/members`);
|
|
165
|
+
}
|
|
166
|
+
meetingTransferHost(e, t) {
|
|
167
|
+
return this.request(`/meeting/${e}/transfer-host`, {
|
|
168
|
+
method: "POST",
|
|
169
|
+
body: JSON.stringify({ newHostUid: t })
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
sendMessage(e) {
|
|
173
|
+
return this.request("/messages", {
|
|
174
|
+
method: "POST",
|
|
175
|
+
body: JSON.stringify(e)
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
ackMessage(e) {
|
|
179
|
+
return this.request(`/messages/${e}/ack`, { method: "POST" });
|
|
180
|
+
}
|
|
181
|
+
revokeMessage(e) {
|
|
182
|
+
return this.request(`/messages/${e}/revoke`, { method: "POST" });
|
|
183
|
+
}
|
|
184
|
+
listMessages(e, t = 0, n = 50) {
|
|
185
|
+
return this.request(`/conversations/${e}/messages?sinceSeq=${t}&limit=${n}`);
|
|
186
|
+
}
|
|
187
|
+
markRead(e, t) {
|
|
188
|
+
return this.request(`/conversations/${e}/read`, {
|
|
189
|
+
method: "POST",
|
|
190
|
+
body: JSON.stringify({ lastReadSeq: t })
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
listConversations() {
|
|
194
|
+
return this.request("/conversations");
|
|
195
|
+
}
|
|
196
|
+
getUploadUrl(e) {
|
|
197
|
+
return this.request("/media/upload-url", {
|
|
198
|
+
method: "POST",
|
|
199
|
+
body: JSON.stringify(e)
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
checkMd5(e) {
|
|
203
|
+
return this.request("/media/check-md5", {
|
|
204
|
+
method: "POST",
|
|
205
|
+
body: JSON.stringify({ md5: e })
|
|
206
|
+
});
|
|
207
|
+
}
|
|
208
|
+
}, o = class {
|
|
209
|
+
transport;
|
|
210
|
+
sessionToken = null;
|
|
211
|
+
centrifugoToken = null;
|
|
212
|
+
expiresAt = 0;
|
|
213
|
+
refreshTimer = null;
|
|
214
|
+
refreshAheadMs = 60 * 1e3;
|
|
215
|
+
constructor(e) {
|
|
216
|
+
this.transport = e;
|
|
217
|
+
}
|
|
218
|
+
getToken() {
|
|
219
|
+
return this.sessionToken;
|
|
220
|
+
}
|
|
221
|
+
isExpired() {
|
|
222
|
+
return Date.now() >= this.expiresAt - this.refreshAheadMs;
|
|
223
|
+
}
|
|
224
|
+
async login(e, t, n = 4, r) {
|
|
225
|
+
let i = await this.transport.login(e, t, n, r);
|
|
226
|
+
return this.sessionToken = i.token, this.expiresAt = i.expiresAt * 1e3, this.scheduleRefresh(), i;
|
|
227
|
+
}
|
|
228
|
+
setExternalToken(e, t) {
|
|
229
|
+
this.sessionToken = e, this.expiresAt = t * 1e3, this.scheduleRefresh();
|
|
230
|
+
}
|
|
231
|
+
async getCentrifugoToken(e, t) {
|
|
232
|
+
if (this.centrifugoToken) return this.centrifugoToken;
|
|
233
|
+
let n = await this.transport.getCentrifugoToken(e, t);
|
|
234
|
+
return this.centrifugoToken = n.token, setTimeout(() => {
|
|
235
|
+
this.centrifugoToken = null;
|
|
236
|
+
}, 240 * 1e3), n.token;
|
|
237
|
+
}
|
|
238
|
+
async refresh() {
|
|
239
|
+
for (let e = 0; e < 3; e++) try {
|
|
240
|
+
let e = await this.transport.refresh();
|
|
241
|
+
this.sessionToken = e.token, this.expiresAt = Date.now() + 1440 * 60 * 1e3, this.scheduleRefresh();
|
|
242
|
+
return;
|
|
243
|
+
} catch (t) {
|
|
244
|
+
let n = String(t?.message || t);
|
|
245
|
+
if (n.includes("401") || n.includes("kicked") || n.includes("expired")) {
|
|
246
|
+
console.error("[atim] refresh 失败(session 失效),需重新登录", t), this.logout();
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
console.warn(`[atim] refresh token 失败(第 ${e + 1} 次),5s 后重试`, t), await new Promise((e) => setTimeout(e, 5e3));
|
|
250
|
+
}
|
|
251
|
+
console.error("[atim] refresh token 重试 3 次仍失败,清 token"), this.logout();
|
|
252
|
+
}
|
|
253
|
+
scheduleRefresh() {
|
|
254
|
+
this.refreshTimer && clearTimeout(this.refreshTimer);
|
|
255
|
+
let e = Math.max(this.expiresAt - Date.now() - this.refreshAheadMs, 60 * 1e3);
|
|
256
|
+
this.refreshTimer = setTimeout(() => this.refresh(), e);
|
|
257
|
+
}
|
|
258
|
+
logout() {
|
|
259
|
+
this.refreshTimer &&= (clearTimeout(this.refreshTimer), null), this.sessionToken = null, this.centrifugoToken = null;
|
|
260
|
+
}
|
|
261
|
+
}, s = class extends r {
|
|
262
|
+
client = null;
|
|
263
|
+
subscriptions = /* @__PURE__ */ new Map();
|
|
264
|
+
refCount = /* @__PURE__ */ new Map();
|
|
265
|
+
wsUrl;
|
|
266
|
+
uid = "";
|
|
267
|
+
tokenGetter;
|
|
268
|
+
currentState = "disconnected";
|
|
269
|
+
constructor(e, t) {
|
|
270
|
+
super(), this.wsUrl = e, this.tokenGetter = t;
|
|
271
|
+
}
|
|
272
|
+
get state() {
|
|
273
|
+
return this.currentState;
|
|
274
|
+
}
|
|
275
|
+
async connect(t) {
|
|
276
|
+
this.uid = t, this.client = new e(this.wsUrl, { getToken: async () => await this.tokenGetter() }), this.client.on("connected", () => {
|
|
277
|
+
this.currentState = "connected", this.emit("connection:state", "connected"), this.subscribe(`personal#${t}`);
|
|
278
|
+
}), this.client.on("disconnected", (e) => {
|
|
279
|
+
this.currentState = "disconnected", this.emit("connection:state", "disconnected"), console.warn("[atim] centrifugo disconnected", e?.reason);
|
|
280
|
+
}), this.client.on("error", (e) => {
|
|
281
|
+
console.error("[atim] centrifugo error", e);
|
|
282
|
+
}), this.client.connect();
|
|
283
|
+
}
|
|
284
|
+
async subscribe(e) {
|
|
285
|
+
if (!this.client) return null;
|
|
286
|
+
let t = this.refCount.get(e) || 0;
|
|
287
|
+
if (this.refCount.set(e, t + 1), this.subscriptions.has(e)) return this.subscriptions.get(e);
|
|
288
|
+
let n = this.client.newSubscription(e);
|
|
289
|
+
return n.on("publication", (t) => {
|
|
290
|
+
this.handlePublication(e, t);
|
|
291
|
+
}), n.subscribe(), this.subscriptions.set(e, n), n;
|
|
292
|
+
}
|
|
293
|
+
unsubscribe(e) {
|
|
294
|
+
let t = this.refCount.get(e) || 0;
|
|
295
|
+
t <= 1 ? this.refCount.delete(e) : this.refCount.set(e, t - 1);
|
|
296
|
+
}
|
|
297
|
+
async publish(e, t) {
|
|
298
|
+
let n = this.subscriptions.get(e);
|
|
299
|
+
if (!n) throw Error(`未订阅通道: ${e}`);
|
|
300
|
+
await n.publish(t);
|
|
301
|
+
}
|
|
302
|
+
handlePublication(e, t) {
|
|
303
|
+
let n = t.data, r = n.type;
|
|
304
|
+
switch (r) {
|
|
305
|
+
case "call_invite":
|
|
306
|
+
this.emit("call:invite", n);
|
|
307
|
+
break;
|
|
308
|
+
case "call_accepted":
|
|
309
|
+
this.emit("call:accepted", n);
|
|
310
|
+
break;
|
|
311
|
+
case "call_rejected":
|
|
312
|
+
this.emit("call:rejected", n);
|
|
313
|
+
break;
|
|
314
|
+
case "call_cancelled":
|
|
315
|
+
this.emit("call:cancelled", n);
|
|
316
|
+
break;
|
|
317
|
+
case "call_busy":
|
|
318
|
+
this.emit("call:busy", n);
|
|
319
|
+
break;
|
|
320
|
+
case "call_no_answer":
|
|
321
|
+
this.emit("call:no_answer", n);
|
|
322
|
+
break;
|
|
323
|
+
case "call_ended":
|
|
324
|
+
this.emit("call:ended", n);
|
|
325
|
+
break;
|
|
326
|
+
case "meeting_invite":
|
|
327
|
+
this.emit("meeting:invite", n);
|
|
328
|
+
break;
|
|
329
|
+
case "meeting_kicked":
|
|
330
|
+
this.emit("meeting:kicked", n);
|
|
331
|
+
break;
|
|
332
|
+
case "meeting_host_changed":
|
|
333
|
+
this.emit("meeting:hostChanged", n);
|
|
334
|
+
break;
|
|
335
|
+
case "meeting_dissolved":
|
|
336
|
+
this.emit("meeting:dissolved", n);
|
|
337
|
+
break;
|
|
338
|
+
case "message":
|
|
339
|
+
this.emit("message", n);
|
|
340
|
+
break;
|
|
341
|
+
case "message_preview":
|
|
342
|
+
this.emit("message_preview", n);
|
|
343
|
+
break;
|
|
344
|
+
case "message_revoked":
|
|
345
|
+
this.emit("message:revoked", n);
|
|
346
|
+
break;
|
|
347
|
+
case "read_receipt":
|
|
348
|
+
this.emit("read_receipt", n);
|
|
349
|
+
break;
|
|
350
|
+
case "presence:update":
|
|
351
|
+
{
|
|
352
|
+
let e = n.changes || [];
|
|
353
|
+
for (let t of e) this.emit("presence:update", {
|
|
354
|
+
uid: t.uid,
|
|
355
|
+
online: t.online
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
break;
|
|
359
|
+
case "session:kicked":
|
|
360
|
+
this.emit("session:kicked", n);
|
|
361
|
+
break;
|
|
362
|
+
case "group:memberJoined":
|
|
363
|
+
this.emit("group:memberJoined", n);
|
|
364
|
+
break;
|
|
365
|
+
case "group:kicked":
|
|
366
|
+
this.emit("group:kicked", n);
|
|
367
|
+
break;
|
|
368
|
+
case "group:memberLeft":
|
|
369
|
+
this.emit("group:memberLeft", n);
|
|
370
|
+
break;
|
|
371
|
+
case "group:profileChanged":
|
|
372
|
+
this.emit("group:profileChanged", n);
|
|
373
|
+
break;
|
|
374
|
+
default: console.debug("[atim] 未知 publication type", r, e);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
disconnect() {
|
|
378
|
+
this.client &&= (this.client.disconnect(), null), this.subscriptions.clear(), this.currentState = "disconnected";
|
|
379
|
+
}
|
|
380
|
+
}, c = class extends r {
|
|
381
|
+
callId;
|
|
382
|
+
callType;
|
|
383
|
+
roomName;
|
|
384
|
+
identity;
|
|
385
|
+
peerIdentity;
|
|
386
|
+
room;
|
|
387
|
+
livekitWs;
|
|
388
|
+
iceServers;
|
|
389
|
+
ended = !1;
|
|
390
|
+
sessionListeners = /* @__PURE__ */ new Map();
|
|
391
|
+
constructor(e) {
|
|
392
|
+
super(), this.callId = e.callId, this.callType = e.callType, this.roomName = e.roomName, this.identity = e.identity, this.token = e.token, this.livekitWs = e.livekitWs, this.iceServers = e.iceServers, this.room = new t({
|
|
393
|
+
adaptiveStream: !0,
|
|
394
|
+
dynacast: !0
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
async connect() {
|
|
398
|
+
this.room.on("participantConnected", (e) => {
|
|
399
|
+
this.peerIdentity = e.identity, this.emitSession("remoteConnected", { identity: e.identity });
|
|
400
|
+
}).on("participantDisconnected", (e) => {
|
|
401
|
+
this.emitSession("remoteDisconnected", { identity: e.identity }), this.end("user_left");
|
|
402
|
+
}).on("disconnected", () => {
|
|
403
|
+
this.ended || this.end("user_left");
|
|
404
|
+
}).on("trackSubscribed", (e, t, r) => {
|
|
405
|
+
e.kind === n.Kind.Audio && e.attach(), this.emitSession("remoteTrackSubscribed", {
|
|
406
|
+
kind: e.kind,
|
|
407
|
+
identity: r.identity
|
|
408
|
+
});
|
|
409
|
+
}), await this.room.connect(this.livekitWs, this.token, { rtcConfig: { iceServers: this.iceServers } }), this.emitSession("connected", {});
|
|
410
|
+
}
|
|
411
|
+
async enableDevices() {
|
|
412
|
+
let e = [], t = !1, n = !1;
|
|
413
|
+
try {
|
|
414
|
+
await this.room.localParticipant.setMicrophoneEnabled(!0), t = !0;
|
|
415
|
+
} catch (t) {
|
|
416
|
+
let n = t?.message || String(t);
|
|
417
|
+
e.push(`mic: ${n}`), console.warn("[atim] 麦克风开启失败,通话继续(音频不可用)", t);
|
|
418
|
+
}
|
|
419
|
+
if (this.callType === "video") try {
|
|
420
|
+
await this.room.localParticipant.setCameraEnabled(!0), n = !0;
|
|
421
|
+
} catch (t) {
|
|
422
|
+
let n = t?.message || String(t);
|
|
423
|
+
e.push(`camera: ${n}`), console.warn("[atim] 摄像头开启失败,通话继续(视频不可用)", t);
|
|
424
|
+
}
|
|
425
|
+
return this.emitSession("devices", {
|
|
426
|
+
mic: t,
|
|
427
|
+
camera: n,
|
|
428
|
+
errors: e
|
|
429
|
+
}), {
|
|
430
|
+
mic: t,
|
|
431
|
+
camera: n,
|
|
432
|
+
errors: e
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
attachLocalVideo(e) {
|
|
436
|
+
let t = this.room.localParticipant.getTrackPublication(n.Source.Camera);
|
|
437
|
+
t && t.track && t.track.attach(e);
|
|
438
|
+
}
|
|
439
|
+
attachRemoteVideo(e) {
|
|
440
|
+
for (let [, t] of this.room.remoteParticipants) {
|
|
441
|
+
let r = t.getTrackPublication(n.Source.Camera);
|
|
442
|
+
if (r) {
|
|
443
|
+
if (r.track) {
|
|
444
|
+
r.track.attach(e);
|
|
445
|
+
return;
|
|
446
|
+
}
|
|
447
|
+
r.isSubscribed || r.setSubscribed(!0);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
detachAllVideoFromEl(e) {
|
|
452
|
+
if (!e) return;
|
|
453
|
+
let t = this.room.localParticipant.getTrackPublication(n.Source.Camera);
|
|
454
|
+
t && t.track && t.track.detach(e);
|
|
455
|
+
for (let [, t] of this.room.remoteParticipants) {
|
|
456
|
+
let r = t.getTrackPublication(n.Source.Camera);
|
|
457
|
+
r && r.track && r.track.detach(e);
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
async setMicEnabled(e) {
|
|
461
|
+
await this.room.localParticipant.setMicrophoneEnabled(e);
|
|
462
|
+
}
|
|
463
|
+
async setCameraEnabled(e) {
|
|
464
|
+
await this.room.localParticipant.setCameraEnabled(e);
|
|
465
|
+
}
|
|
466
|
+
async hangup(e, t = "user_left") {
|
|
467
|
+
if (!this.ended) {
|
|
468
|
+
this.ended = !0;
|
|
469
|
+
try {
|
|
470
|
+
await e.callEnd(this.callId, t);
|
|
471
|
+
} catch (e) {
|
|
472
|
+
console.error("[atim] callEnd 失败", e);
|
|
473
|
+
}
|
|
474
|
+
await this.room.disconnect(), this.emitSession("ended", { reason: t });
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
async end(e) {
|
|
478
|
+
this.ended || (this.ended = !0, await this.room.disconnect(), this.emitSession("ended", { reason: e }));
|
|
479
|
+
}
|
|
480
|
+
registerRpcMethod(e, t) {
|
|
481
|
+
this.room.localParticipant.registerRpcMethod(e, (e) => t(e.payload));
|
|
482
|
+
}
|
|
483
|
+
async performRpc(e, t, n) {
|
|
484
|
+
return await this.room.localParticipant.performRpc({
|
|
485
|
+
destinationIdentity: e,
|
|
486
|
+
method: t,
|
|
487
|
+
payload: n
|
|
488
|
+
});
|
|
489
|
+
}
|
|
490
|
+
emitSession(e, t) {
|
|
491
|
+
let n = this.sessionListeners.get(e);
|
|
492
|
+
if (n) for (let r of n) try {
|
|
493
|
+
r(t);
|
|
494
|
+
} catch (t) {
|
|
495
|
+
console.error(`[atim] session handler error: ${e}`, t);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
onSession(e, t) {
|
|
499
|
+
this.sessionListeners.has(e) || this.sessionListeners.set(e, /* @__PURE__ */ new Set()), this.sessionListeners.get(e).add(t);
|
|
500
|
+
}
|
|
501
|
+
get isConnected() {
|
|
502
|
+
return this.room.state === "connected";
|
|
503
|
+
}
|
|
504
|
+
getToken() {
|
|
505
|
+
return this.token;
|
|
506
|
+
}
|
|
507
|
+
token;
|
|
508
|
+
}, l = class {
|
|
509
|
+
transport;
|
|
510
|
+
livekitWs;
|
|
511
|
+
identity;
|
|
512
|
+
iceServers;
|
|
513
|
+
currentSession = null;
|
|
514
|
+
constructor(e, t, n, r) {
|
|
515
|
+
this.transport = e, this.livekitWs = t, this.identity = n, this.iceServers = r;
|
|
516
|
+
}
|
|
517
|
+
async init(e, t) {
|
|
518
|
+
let n = await this.transport.callInit(e, t), r = new c({
|
|
519
|
+
callId: n.callId,
|
|
520
|
+
callType: t,
|
|
521
|
+
roomName: n.roomName,
|
|
522
|
+
identity: this.identity,
|
|
523
|
+
token: n.token,
|
|
524
|
+
livekitWs: this.livekitWs,
|
|
525
|
+
iceServers: this.iceServers
|
|
526
|
+
});
|
|
527
|
+
return this.currentSession = r, r;
|
|
528
|
+
}
|
|
529
|
+
async accept(e, t) {
|
|
530
|
+
let n = await this.transport.callAccept(e), r = new c({
|
|
531
|
+
callId: e,
|
|
532
|
+
callType: t,
|
|
533
|
+
roomName: n.roomName,
|
|
534
|
+
identity: this.identity,
|
|
535
|
+
token: n.token,
|
|
536
|
+
livekitWs: this.livekitWs,
|
|
537
|
+
iceServers: this.iceServers
|
|
538
|
+
});
|
|
539
|
+
this.currentSession = r;
|
|
540
|
+
try {
|
|
541
|
+
await r.connect();
|
|
542
|
+
} catch (t) {
|
|
543
|
+
throw this.currentSession = null, this.transport.callEnd(e, "user_left").catch(() => {}), t;
|
|
544
|
+
}
|
|
545
|
+
return r.enableDevices().catch((e) => {
|
|
546
|
+
console.warn("[atim] enableDevices 异常", e);
|
|
547
|
+
}), r;
|
|
548
|
+
}
|
|
549
|
+
async end(e = "user_left", t) {
|
|
550
|
+
if (this.currentSession) await this.currentSession.hangup(this.transport, e), this.currentSession = null;
|
|
551
|
+
else if (t) try {
|
|
552
|
+
await this.transport.callEnd(t, e);
|
|
553
|
+
} catch (e) {
|
|
554
|
+
console.warn("[atim] callEnd 失败(不阻塞)", e);
|
|
555
|
+
}
|
|
556
|
+
}
|
|
557
|
+
}, u = class e extends r {
|
|
558
|
+
meetingId;
|
|
559
|
+
title;
|
|
560
|
+
roomName;
|
|
561
|
+
identity;
|
|
562
|
+
isHost;
|
|
563
|
+
hostIdentity;
|
|
564
|
+
room;
|
|
565
|
+
livekitWs;
|
|
566
|
+
token;
|
|
567
|
+
iceServers;
|
|
568
|
+
ended = !1;
|
|
569
|
+
sessionListeners = /* @__PURE__ */ new Map();
|
|
570
|
+
membersCache = [];
|
|
571
|
+
membersCacheAt = 0;
|
|
572
|
+
static MEMBERS_CACHE_TTL = 2e3;
|
|
573
|
+
pendingAttach = /* @__PURE__ */ new Map();
|
|
574
|
+
constructor(e) {
|
|
575
|
+
super(), this.meetingId = e.meetingId, this.title = e.title, this.roomName = e.roomName, this.identity = e.identity, this.token = e.token, this.isHost = e.isHost, this.hostIdentity = e.hostIdentity, this.livekitWs = e.livekitWs, this.iceServers = e.iceServers, this.room = new t({
|
|
576
|
+
adaptiveStream: !0,
|
|
577
|
+
dynacast: !0
|
|
578
|
+
});
|
|
579
|
+
}
|
|
580
|
+
get isConnected() {
|
|
581
|
+
return this.room.state === "connected";
|
|
582
|
+
}
|
|
583
|
+
async connect() {
|
|
584
|
+
this.room.on("participantConnected", (e) => {
|
|
585
|
+
if (this.pendingAttach.has(e.identity)) {
|
|
586
|
+
let t = e.getTrackPublication(n.Source.Camera);
|
|
587
|
+
t && !t.isSubscribed && t.setSubscribed(!0);
|
|
588
|
+
}
|
|
589
|
+
this.emitSession("participantJoined", { identity: e.identity });
|
|
590
|
+
}).on("participantDisconnected", (e) => {
|
|
591
|
+
this.emitSession("participantLeft", {
|
|
592
|
+
identity: e.identity,
|
|
593
|
+
reason: "leave"
|
|
594
|
+
});
|
|
595
|
+
}).on("disconnected", () => {
|
|
596
|
+
this.ended || this.end();
|
|
597
|
+
}).on("trackSubscribed", (e, t, r) => {
|
|
598
|
+
if (e.kind === "video") {
|
|
599
|
+
let t = this.pendingAttach.get(r.identity);
|
|
600
|
+
t && (e.attach(t), this.pendingAttach.delete(r.identity));
|
|
601
|
+
} else e.kind === n.Kind.Audio && e.attach();
|
|
602
|
+
this.emitSession("participantTrackSubscribed", {
|
|
603
|
+
kind: e.kind,
|
|
604
|
+
identity: r.identity
|
|
605
|
+
});
|
|
606
|
+
}).on("activeSpeakersChanged", (e) => {
|
|
607
|
+
let t = e.map((e) => e.identity);
|
|
608
|
+
this.emitSession("activeSpeakersChanged", { identities: t });
|
|
609
|
+
}), await this.room.connect(this.livekitWs, this.token, { rtcConfig: { iceServers: this.iceServers } });
|
|
610
|
+
let e = this.room.localParticipant.identity;
|
|
611
|
+
e && (this.identity = e, this.hostIdentity ||= e), this.emitSession("connected", {});
|
|
612
|
+
}
|
|
613
|
+
async enableDevices() {
|
|
614
|
+
let e = [], t = !1, n = !1;
|
|
615
|
+
try {
|
|
616
|
+
await this.room.localParticipant.setMicrophoneEnabled(!0), t = !0;
|
|
617
|
+
} catch (t) {
|
|
618
|
+
e.push(`mic: ${t?.message || String(t)}`), console.warn("[atim] 麦克风开启失败,会议继续(音频不可用)", t);
|
|
619
|
+
}
|
|
620
|
+
try {
|
|
621
|
+
await this.room.localParticipant.setCameraEnabled(!0), n = !0;
|
|
622
|
+
} catch (t) {
|
|
623
|
+
e.push(`camera: ${t?.message || String(t)}`), console.warn("[atim] 摄像头开启失败,会议继续(视频不可用)", t);
|
|
624
|
+
}
|
|
625
|
+
return this.emitSession("devices", {
|
|
626
|
+
mic: t,
|
|
627
|
+
camera: n,
|
|
628
|
+
errors: e
|
|
629
|
+
}), {
|
|
630
|
+
mic: t,
|
|
631
|
+
camera: n,
|
|
632
|
+
errors: e
|
|
633
|
+
};
|
|
634
|
+
}
|
|
635
|
+
async kick(e, t) {
|
|
636
|
+
await e.meetingKick(this.meetingId, t);
|
|
637
|
+
}
|
|
638
|
+
async leave(e) {
|
|
639
|
+
if (!this.ended) {
|
|
640
|
+
this.ended = !0;
|
|
641
|
+
try {
|
|
642
|
+
await e.meetingLeave(this.meetingId);
|
|
643
|
+
} catch (e) {
|
|
644
|
+
console.error("[atim] meetingLeave 失败", e);
|
|
645
|
+
}
|
|
646
|
+
await this.room.disconnect(), this.emitSession("ended", {});
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
async end() {
|
|
650
|
+
this.ended || (this.ended = !0, await this.room.disconnect(), this.emitSession("ended", {}));
|
|
651
|
+
}
|
|
652
|
+
getRemoteParticipants() {
|
|
653
|
+
let e = [];
|
|
654
|
+
for (let [, t] of this.room.remoteParticipants) e.push({
|
|
655
|
+
identity: t.identity,
|
|
656
|
+
audioEnabled: t.isMicrophoneEnabled,
|
|
657
|
+
videoEnabled: t.isCameraEnabled,
|
|
658
|
+
isSpeaking: t.isSpeaking
|
|
659
|
+
});
|
|
660
|
+
return e;
|
|
661
|
+
}
|
|
662
|
+
attachLocalVideo(e) {
|
|
663
|
+
if ((() => {
|
|
664
|
+
let t = this.room.localParticipant.getTrackPublication(n.Source.Camera);
|
|
665
|
+
return t && t.track ? (t.track.attach(e), !0) : !1;
|
|
666
|
+
})()) return;
|
|
667
|
+
let t = (r) => {
|
|
668
|
+
r.source === n.Source.Camera && r.track && (r.track.attach(e), this.room.localParticipant.off("localTrackPublished", t));
|
|
669
|
+
};
|
|
670
|
+
this.room.localParticipant.on("localTrackPublished", t);
|
|
671
|
+
}
|
|
672
|
+
attachParticipantVideo(e, t) {
|
|
673
|
+
let r = this.room.remoteParticipants.get(e);
|
|
674
|
+
if (!r) {
|
|
675
|
+
this.pendingAttach.set(e, t);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
let i = r.getTrackPublication(n.Source.Camera);
|
|
679
|
+
if (!i) {
|
|
680
|
+
this.pendingAttach.set(e, t);
|
|
681
|
+
return;
|
|
682
|
+
}
|
|
683
|
+
if (i.track) {
|
|
684
|
+
i.track.attach(t), this.pendingAttach.delete(e);
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
this.pendingAttach.set(e, t), i.isSubscribed || i.setSubscribed(!0);
|
|
688
|
+
}
|
|
689
|
+
async setMicEnabled(e) {
|
|
690
|
+
await this.room.localParticipant.setMicrophoneEnabled(e);
|
|
691
|
+
}
|
|
692
|
+
async setCameraEnabled(e) {
|
|
693
|
+
await this.room.localParticipant.setCameraEnabled(e);
|
|
694
|
+
}
|
|
695
|
+
async setSpeakerMuted(e) {
|
|
696
|
+
for (let [, t] of this.room.remoteParticipants) for (let [, r] of t.audioTrackPublications) {
|
|
697
|
+
let t = r.track;
|
|
698
|
+
t && t.kind === n.Kind.Audio && t.setVolume(+!e);
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
async refreshMembers(t, n = !1) {
|
|
702
|
+
let r = Date.now();
|
|
703
|
+
if (!n && r - this.membersCacheAt < e.MEMBERS_CACHE_TTL && this.membersCache.length > 0) return this.membersCache;
|
|
704
|
+
let i = await t.meetingMembers(this.meetingId);
|
|
705
|
+
return this.membersCache = i.members || [], this.membersCacheAt = r, i.hostUid && i.hostUid !== this.hostIdentity && (this.hostIdentity = i.hostUid), this.membersCache;
|
|
706
|
+
}
|
|
707
|
+
getMembers() {
|
|
708
|
+
return this.membersCache;
|
|
709
|
+
}
|
|
710
|
+
async transferHost(e, t) {
|
|
711
|
+
await e.meetingTransferHost(this.meetingId, t), this.hostIdentity = t, this.emitSession("hostChanged", { hostUid: t });
|
|
712
|
+
}
|
|
713
|
+
onSession(e, t) {
|
|
714
|
+
this.sessionListeners.has(e) || this.sessionListeners.set(e, /* @__PURE__ */ new Set()), this.sessionListeners.get(e).add(t);
|
|
715
|
+
}
|
|
716
|
+
emitSession(e, t) {
|
|
717
|
+
let n = this.sessionListeners.get(e);
|
|
718
|
+
if (n) for (let r of n) try {
|
|
719
|
+
r(t);
|
|
720
|
+
} catch (t) {
|
|
721
|
+
console.error(`[atim] session handler error: ${e}`, t);
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}, d = class {
|
|
725
|
+
transport;
|
|
726
|
+
livekitWs;
|
|
727
|
+
identity;
|
|
728
|
+
iceServers;
|
|
729
|
+
currentSession = null;
|
|
730
|
+
constructor(e, t, n, r) {
|
|
731
|
+
this.transport = e, this.livekitWs = t, this.identity = n, this.iceServers = r;
|
|
732
|
+
}
|
|
733
|
+
async create(e, t) {
|
|
734
|
+
let n = await this.transport.meetingCreate({
|
|
735
|
+
inviteeUids: e,
|
|
736
|
+
title: t
|
|
737
|
+
}), r = new u({
|
|
738
|
+
meetingId: n.meetingId,
|
|
739
|
+
title: t,
|
|
740
|
+
roomName: n.roomName,
|
|
741
|
+
identity: this.identity,
|
|
742
|
+
token: n.token,
|
|
743
|
+
isHost: !0,
|
|
744
|
+
hostIdentity: n.hostUid || this.identity,
|
|
745
|
+
livekitWs: this.livekitWs,
|
|
746
|
+
iceServers: this.iceServers
|
|
747
|
+
});
|
|
748
|
+
this.currentSession = r;
|
|
749
|
+
try {
|
|
750
|
+
await r.connect(), r.enableDevices().catch((e) => console.warn("[atim] enableDevices 异常", e));
|
|
751
|
+
} catch (e) {
|
|
752
|
+
throw this.currentSession = null, this.transport.meetingLeave(n.meetingId).catch(() => {}), e;
|
|
753
|
+
}
|
|
754
|
+
return {
|
|
755
|
+
meetingId: n.meetingId,
|
|
756
|
+
session: r,
|
|
757
|
+
busyInvitees: n.busyInvitees
|
|
758
|
+
};
|
|
759
|
+
}
|
|
760
|
+
async join(e, t = "") {
|
|
761
|
+
let n = await this.transport.meetingJoin(e), r = new u({
|
|
762
|
+
meetingId: e,
|
|
763
|
+
title: t,
|
|
764
|
+
roomName: n.roomName,
|
|
765
|
+
identity: this.identity,
|
|
766
|
+
token: n.token,
|
|
767
|
+
isHost: !1,
|
|
768
|
+
hostIdentity: n.hostUid || "",
|
|
769
|
+
livekitWs: this.livekitWs,
|
|
770
|
+
iceServers: this.iceServers
|
|
771
|
+
});
|
|
772
|
+
this.currentSession = r;
|
|
773
|
+
try {
|
|
774
|
+
await r.connect(), r.enableDevices().catch((e) => console.warn("[atim] enableDevices 异常", e));
|
|
775
|
+
} catch (t) {
|
|
776
|
+
throw this.currentSession = null, this.transport.meetingLeave(e).catch(() => {}), t;
|
|
777
|
+
}
|
|
778
|
+
return r;
|
|
779
|
+
}
|
|
780
|
+
async respond(e, t, n) {
|
|
781
|
+
let r = await this.transport.meetingRespond(e, t);
|
|
782
|
+
if (t !== "accept" || !r?.token || !r?.roomName) return null;
|
|
783
|
+
let i = new u({
|
|
784
|
+
meetingId: e,
|
|
785
|
+
title: r.title || n || "",
|
|
786
|
+
roomName: r.roomName,
|
|
787
|
+
identity: this.identity,
|
|
788
|
+
token: r.token,
|
|
789
|
+
isHost: !1,
|
|
790
|
+
hostIdentity: r.hostUid || "",
|
|
791
|
+
livekitWs: this.livekitWs,
|
|
792
|
+
iceServers: this.iceServers
|
|
793
|
+
});
|
|
794
|
+
this.currentSession = i;
|
|
795
|
+
try {
|
|
796
|
+
await i.connect(), i.enableDevices().catch((e) => console.warn("[atim] enableDevices 异常", e));
|
|
797
|
+
} catch (t) {
|
|
798
|
+
throw this.currentSession = null, this.transport.meetingLeave(e).catch(() => {}), t;
|
|
799
|
+
}
|
|
800
|
+
return i;
|
|
801
|
+
}
|
|
802
|
+
async leave() {
|
|
803
|
+
this.currentSession &&= (await this.currentSession.leave(this.transport), null);
|
|
804
|
+
}
|
|
805
|
+
async dissolve() {
|
|
806
|
+
if (!this.currentSession) return !1;
|
|
807
|
+
let e = this.currentSession.meetingId;
|
|
808
|
+
return await this.transport.meetingDissolve(e), this.currentSession = null, !0;
|
|
809
|
+
}
|
|
810
|
+
async getParticipantCount() {
|
|
811
|
+
return this.currentSession ? (await this.transport.meetingParticipantCount(this.currentSession.meetingId)).count : 0;
|
|
812
|
+
}
|
|
813
|
+
async fetchPendingInvites() {
|
|
814
|
+
try {
|
|
815
|
+
return (await this.transport.meetingPendingInvites()).invites || [];
|
|
816
|
+
} catch (e) {
|
|
817
|
+
return console.warn("[atim] fetchPendingInvites 失败", e), [];
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
async reinvite(e, t) {
|
|
821
|
+
await this.transport.meetingReinvite(e, t);
|
|
822
|
+
}
|
|
823
|
+
async getMembers(e = !1) {
|
|
824
|
+
return this.currentSession ? this.currentSession.refreshMembers(this.transport, e) : [];
|
|
825
|
+
}
|
|
826
|
+
}, f = class {
|
|
827
|
+
transport;
|
|
828
|
+
realtime;
|
|
829
|
+
uid;
|
|
830
|
+
peerUidCache = /* @__PURE__ */ new Map();
|
|
831
|
+
constructor(e, t, n) {
|
|
832
|
+
this.transport = e, this.realtime = t, this.uid = n;
|
|
833
|
+
}
|
|
834
|
+
async resolvePeerUUID(e) {
|
|
835
|
+
let t = this.peerUidCache.get(e);
|
|
836
|
+
if (t) return t;
|
|
837
|
+
let n = (await this.transport.usersByExternal([e])).users[e];
|
|
838
|
+
if (!n || !n.uid) throw Error(`peer not found: ${e}`);
|
|
839
|
+
return this.peerUidCache.set(e, n.uid), n.uid;
|
|
840
|
+
}
|
|
841
|
+
async send(e) {
|
|
842
|
+
let t = {
|
|
843
|
+
to: e.to,
|
|
844
|
+
type: e.type,
|
|
845
|
+
body: e.body,
|
|
846
|
+
clientMid: crypto.randomUUID(),
|
|
847
|
+
mediaId: e.mediaId,
|
|
848
|
+
replyToId: e.replyToId
|
|
849
|
+
}, n = await this.transport.sendMessage(t);
|
|
850
|
+
return await this.transport.ackMessage(n.msgId).catch(() => {}), n;
|
|
851
|
+
}
|
|
852
|
+
async sendMedia(e, t) {
|
|
853
|
+
let { mediaId: n } = await t.upload(e.file, e.file.type);
|
|
854
|
+
return await this.send({
|
|
855
|
+
to: e.to,
|
|
856
|
+
type: e.type,
|
|
857
|
+
mediaId: n
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
async history(e, t = 0, n = 50) {
|
|
861
|
+
return (await this.transport.listMessages(e, t, n)).messages;
|
|
862
|
+
}
|
|
863
|
+
async ack(e) {
|
|
864
|
+
await this.transport.ackMessage(e);
|
|
865
|
+
}
|
|
866
|
+
async markRead(e, t) {
|
|
867
|
+
await this.transport.markRead(e, t);
|
|
868
|
+
}
|
|
869
|
+
async revoke(e) {
|
|
870
|
+
await this.transport.revokeMessage(e);
|
|
871
|
+
}
|
|
872
|
+
async typing(e, t) {
|
|
873
|
+
let n = await this.resolvePeerUUID(e), r = this.dialogChannel(n);
|
|
874
|
+
await this.realtime.publish(r, {
|
|
875
|
+
type: "typing",
|
|
876
|
+
from: "",
|
|
877
|
+
isTyping: t
|
|
878
|
+
});
|
|
879
|
+
}
|
|
880
|
+
async listConversations() {
|
|
881
|
+
return (await this.transport.listConversations()).conversations;
|
|
882
|
+
}
|
|
883
|
+
async openDialog(e) {
|
|
884
|
+
let t = await this.resolvePeerUUID(e), n = this.dialogChannel(t);
|
|
885
|
+
await this.realtime.subscribe(n);
|
|
886
|
+
}
|
|
887
|
+
closeDialog(e) {
|
|
888
|
+
let t = this.peerUidCache.get(e);
|
|
889
|
+
if (!t) return;
|
|
890
|
+
let n = this.dialogChannel(t);
|
|
891
|
+
this.realtime.unsubscribe(n);
|
|
892
|
+
}
|
|
893
|
+
dialogChannel(e) {
|
|
894
|
+
return `dialog#${[this.uid, e].sort()[0]},${[this.uid, e].sort()[1]}`;
|
|
895
|
+
}
|
|
896
|
+
}, p = class {
|
|
897
|
+
transport;
|
|
898
|
+
constructor(e) {
|
|
899
|
+
this.transport = e;
|
|
900
|
+
}
|
|
901
|
+
async computeMD5(e) {
|
|
902
|
+
let t = await e.arrayBuffer(), n = await crypto.subtle.digest("SHA-256", t);
|
|
903
|
+
return Array.from(new Uint8Array(n)).map((e) => e.toString(16).padStart(2, "0")).join("");
|
|
904
|
+
}
|
|
905
|
+
async upload(e, t) {
|
|
906
|
+
let n = await this.computeMD5(e), r = await this.transport.checkMd5(n);
|
|
907
|
+
if (r.hit && r.mediaId) return { mediaId: r.mediaId };
|
|
908
|
+
let i = await this.transport.getUploadUrl({
|
|
909
|
+
fileName: e.name,
|
|
910
|
+
contentType: t || e.type,
|
|
911
|
+
fileSize: e.size,
|
|
912
|
+
md5: n
|
|
913
|
+
});
|
|
914
|
+
if (i.uploadUrl && i.uploadUrl.startsWith("http")) {
|
|
915
|
+
let n = await fetch(i.uploadUrl, {
|
|
916
|
+
method: "PUT",
|
|
917
|
+
body: e,
|
|
918
|
+
headers: { "Content-Type": t || e.type }
|
|
919
|
+
});
|
|
920
|
+
if (!n.ok) throw Error(`上传对象存储失败: HTTP ${n.status}`);
|
|
921
|
+
}
|
|
922
|
+
return { mediaId: i.mediaId };
|
|
923
|
+
}
|
|
924
|
+
}, m;
|
|
925
|
+
(function(e) {
|
|
926
|
+
e[e.PC = 1] = "PC", e[e.Android = 2] = "Android", e[e.iOS = 3] = "iOS", e[e.Web = 4] = "Web";
|
|
927
|
+
})(m ||= {});
|
|
928
|
+
//#endregion
|
|
929
|
+
//#region ../contract/dist/group.js
|
|
930
|
+
var h;
|
|
931
|
+
(function(e) {
|
|
932
|
+
e[e.Join = 1] = "Join", e[e.Leave = 2] = "Leave", e[e.Kicked = 3] = "Kicked", e[e.AdminChange = 4] = "AdminChange", e[e.ProfileChange = 5] = "ProfileChange";
|
|
933
|
+
})(h ||= {});
|
|
934
|
+
//#endregion
|
|
935
|
+
//#region ../contract/dist/error.js
|
|
936
|
+
var g = {
|
|
937
|
+
BadBody: 40001,
|
|
938
|
+
BadParam: 40002,
|
|
939
|
+
Unauth: 40101,
|
|
940
|
+
SessionKicked: 40102,
|
|
941
|
+
BadSig: 40103,
|
|
942
|
+
Forbidden: 40301,
|
|
943
|
+
NotFound: 40401,
|
|
944
|
+
Busy: 40901,
|
|
945
|
+
Conflict: 40902,
|
|
946
|
+
Internal: 50001,
|
|
947
|
+
Upstream: 50201
|
|
948
|
+
}, _ = [
|
|
949
|
+
"message",
|
|
950
|
+
"message_preview",
|
|
951
|
+
"message:revoked",
|
|
952
|
+
"read_receipt",
|
|
953
|
+
"typing",
|
|
954
|
+
"call:invite",
|
|
955
|
+
"call:accepted",
|
|
956
|
+
"call:rejected",
|
|
957
|
+
"call:cancelled",
|
|
958
|
+
"call:busy",
|
|
959
|
+
"call:no_answer",
|
|
960
|
+
"call:ended",
|
|
961
|
+
"meeting:invite",
|
|
962
|
+
"meeting:kicked",
|
|
963
|
+
"meeting:hostChanged",
|
|
964
|
+
"meeting:dissolved",
|
|
965
|
+
"group:memberJoined",
|
|
966
|
+
"group:memberLeft",
|
|
967
|
+
"group:kicked",
|
|
968
|
+
"group:announcement",
|
|
969
|
+
"group:profileChanged",
|
|
970
|
+
"session:kicked",
|
|
971
|
+
"connection:state",
|
|
972
|
+
"presence:update"
|
|
973
|
+
], v = class extends r {
|
|
974
|
+
auth;
|
|
975
|
+
transport;
|
|
976
|
+
realtime;
|
|
977
|
+
call;
|
|
978
|
+
meeting;
|
|
979
|
+
message;
|
|
980
|
+
media;
|
|
981
|
+
uid = "";
|
|
982
|
+
appId;
|
|
983
|
+
sessionState = "idle";
|
|
984
|
+
clientConfig = null;
|
|
985
|
+
livekitWs;
|
|
986
|
+
apiBase;
|
|
987
|
+
name = "";
|
|
988
|
+
avatar = "";
|
|
989
|
+
constructor(e) {
|
|
990
|
+
super(), this.appId = e.appId, this.apiBase = e.apiBase, this.livekitWs = e.livekitWs, this.auth = new o(null), this.transport = new a(e.apiBase, () => this.auth.getToken()), this.auth = new o(this.transport), this.realtime = new s(e.centrifugoWs, async () => await this.auth.getCentrifugoToken(this.name, this.avatar));
|
|
991
|
+
}
|
|
992
|
+
async connect(e) {
|
|
993
|
+
e.name && (this.name = e.name), e.avatar && (this.avatar = e.avatar);
|
|
994
|
+
let t = await this.auth.login(this.appId, e.externalId, e.clientType ?? 4, e.deviceName);
|
|
995
|
+
return this.uid = t.uid, t.name && (this.name = t.name), t.avatarUrl && (this.avatar = t.avatarUrl), await this.initAfterAuth(), t;
|
|
996
|
+
}
|
|
997
|
+
async connectWithExternalJwt(e) {
|
|
998
|
+
e.name && (this.name = e.name), e.avatar && (this.avatar = e.avatar), this.uid = e.uid, this.auth.setExternalToken(e.jwt, e.expiresAt), await this.initAfterAuth();
|
|
999
|
+
}
|
|
1000
|
+
async initAfterAuth() {
|
|
1001
|
+
await this.loadClientConfig();
|
|
1002
|
+
let e = this.clientConfig?.iceServers ?? [];
|
|
1003
|
+
this.call = new l(this.transport, this.livekitWs, this.uid, e), this.meeting = new d(this.transport, this.livekitWs, this.uid, e), this.message = new f(this.transport, this.realtime, this.uid), this.media = new p(this.transport), await this.realtime.connect(this.uid);
|
|
1004
|
+
try {
|
|
1005
|
+
let e = await this.transport.getSessionState();
|
|
1006
|
+
this.sessionState = e.state, this.emit("session:state", {
|
|
1007
|
+
state: e.state,
|
|
1008
|
+
callId: e.id,
|
|
1009
|
+
meetingId: e.id
|
|
1010
|
+
});
|
|
1011
|
+
} catch (e) {
|
|
1012
|
+
console.warn("[atim] 同步 sessionState 失败", e);
|
|
1013
|
+
}
|
|
1014
|
+
this.realtime.on("call:invite", () => {}), this.realtime.on("call:busy", () => {
|
|
1015
|
+
this.call.currentSession = null;
|
|
1016
|
+
}), this.realtime.on("call:no_answer", () => {
|
|
1017
|
+
this.call.currentSession = null;
|
|
1018
|
+
}), this.realtime.on("meeting:hostChanged", (e) => {
|
|
1019
|
+
let t = this.meeting?.currentSession;
|
|
1020
|
+
t && e?.meetingId === t.meetingId && e?.hostUid && (t.hostIdentity = e.hostUid);
|
|
1021
|
+
}), this.realtime.on("meeting:dissolved", (e) => {
|
|
1022
|
+
let t = this.meeting?.currentSession;
|
|
1023
|
+
t && e?.meetingId === t.meetingId && t.end().catch((e) => console.warn("[atim] dissolved end session 失败", e));
|
|
1024
|
+
}), this.realtime.on("meeting:kicked", (e) => {
|
|
1025
|
+
let t = this.meeting?.currentSession;
|
|
1026
|
+
t && e?.meetingId === t.meetingId && t.end().catch((e) => console.warn("[atim] kicked end session 失败", e));
|
|
1027
|
+
}), this.realtime.on("session:kicked", () => {
|
|
1028
|
+
console.warn("[atim] 会话被踢(同端新登录),断开连接");
|
|
1029
|
+
try {
|
|
1030
|
+
this.realtime.disconnect();
|
|
1031
|
+
} catch {}
|
|
1032
|
+
this.auth.logout(), this.uid = "";
|
|
1033
|
+
}), this.forwardRealtimeEvents(), this.fetchAndEmitPendingInvites();
|
|
1034
|
+
}
|
|
1035
|
+
async loadClientConfig() {
|
|
1036
|
+
if (!this.clientConfig) try {
|
|
1037
|
+
this.clientConfig = await this.transport.getClientConfig();
|
|
1038
|
+
} catch (e) {
|
|
1039
|
+
console.warn("[atim] 拉取 client/config 失败,使用空 iceServers 兜底", e), this.clientConfig = { iceServers: [] };
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
getConfig() {
|
|
1043
|
+
return this.clientConfig;
|
|
1044
|
+
}
|
|
1045
|
+
async fetchAndEmitPendingInvites() {
|
|
1046
|
+
if (this.meeting) try {
|
|
1047
|
+
let e = await this.meeting.fetchPendingInvites(), t = this.meeting.currentSession?.meetingId;
|
|
1048
|
+
for (let n of e) n.meetingId !== t && this.emit("meeting:invite", {
|
|
1049
|
+
meetingId: n.meetingId,
|
|
1050
|
+
from: n.from,
|
|
1051
|
+
title: n.title,
|
|
1052
|
+
ts: n.ts,
|
|
1053
|
+
variant: n.variant,
|
|
1054
|
+
reinvite: n.variant === "reinvite"
|
|
1055
|
+
});
|
|
1056
|
+
} catch (e) {
|
|
1057
|
+
console.warn("[atim] fetchAndEmitPendingInvites 失败", e);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
forwardRealtimeEvents() {
|
|
1061
|
+
for (let e of _) this.realtime.on(e, (t) => {
|
|
1062
|
+
this.emit(e, t);
|
|
1063
|
+
});
|
|
1064
|
+
}
|
|
1065
|
+
async disconnect() {
|
|
1066
|
+
this.call?.currentSession && await this.call.end("user_left").catch(() => {}), this.meeting?.currentSession && await this.meeting.leave().catch(() => {}), this.realtime.disconnect(), this.auth.logout(), this.removeAllListeners();
|
|
1067
|
+
}
|
|
1068
|
+
};
|
|
1069
|
+
//#endregion
|
|
1070
|
+
export { i as ApiError, g as ApiErrorCode, v as AtimClient, o as AuthManager, l as CallController, c as CallSession, m as ClientType, r as EventEmitter, h as GroupTipType, p as MediaUploader, d as MeetingController, u as MeetingSession, f as MessageController, s as RealtimeConnection, a as Transport };
|
|
1071
|
+
|
|
1072
|
+
//# sourceMappingURL=atim-core.es.js.map
|