@coze/realtime-api 0.0.3 → 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/README.md +6 -0
- package/dist/cjs/index.cjs +42074 -0
- package/dist/cjs/index.cjs.LICENSE.txt +1 -0
- package/dist/esm/index.js +42058 -0
- package/dist/esm/index.js.LICENSE.txt +1 -0
- package/dist/{client.d.ts → types/client.d.ts} +8 -2
- package/dist/{error.d.ts → types/error.d.ts} +10 -2
- package/dist/{event-handler.d.ts → types/event-handler.d.ts} +26 -1
- package/dist/{index.d.ts → types/index.d.ts} +7 -1
- package/dist/{utils.d.ts → types/utils.d.ts} +6 -1
- package/dist/umd/index.js +42077 -0
- package/dist/umd/index.js.LICENSE.txt +1 -0
- package/package.json +27 -10
- package/dist/client.js +0 -242
- package/dist/error.js +0 -36
- package/dist/event-handler.js +0 -126
- package/dist/index.js +0 -258
- package/dist/utils.js +0 -40
@@ -0,0 +1 @@
|
|
1
|
+
/*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */
|
package/package.json
CHANGED
@@ -1,7 +1,18 @@
|
|
1
1
|
{
|
2
2
|
"name": "@coze/realtime-api",
|
3
|
-
"version": "0.0
|
4
|
-
"description": "Coze
|
3
|
+
"version": "1.0.0",
|
4
|
+
"description": "A powerful real-time communication SDK for voice interactions with Coze AI bots | 扣子官方实时通信 SDK,用于与 Coze AI bots 进行语音交互",
|
5
|
+
"keywords": [
|
6
|
+
"coze",
|
7
|
+
"ai",
|
8
|
+
"realtime",
|
9
|
+
"voice",
|
10
|
+
"rtc",
|
11
|
+
"asr",
|
12
|
+
"tts",
|
13
|
+
"ai-agent",
|
14
|
+
"voice-synthesis"
|
15
|
+
],
|
5
16
|
"homepage": "https://github.com/coze-dev/coze-js/packages/realtime-api",
|
6
17
|
"repository": {
|
7
18
|
"type": "git",
|
@@ -12,10 +23,15 @@
|
|
12
23
|
"author": "jackshen <jackshen310@gmail.com>",
|
13
24
|
"type": "module",
|
14
25
|
"exports": {
|
15
|
-
".":
|
26
|
+
".": {
|
27
|
+
"require": "./dist/cjs/index.cjs",
|
28
|
+
"import": "./dist/esm/index.js",
|
29
|
+
"types": "./dist/types/index.d.ts"
|
30
|
+
}
|
16
31
|
},
|
17
|
-
"main": "dist/index.
|
18
|
-
"
|
32
|
+
"main": "dist/cjs/index.cjs",
|
33
|
+
"module": "dist/esm/index.js",
|
34
|
+
"typings": "dist/types/index.d.ts",
|
19
35
|
"files": [
|
20
36
|
"dist",
|
21
37
|
"assets",
|
@@ -24,24 +40,25 @@
|
|
24
40
|
"README.md"
|
25
41
|
],
|
26
42
|
"scripts": {
|
27
|
-
"build": "rm -rf dist &&
|
28
|
-
"buildAll": "npm run build && cd examples/realtime-console && npm run build",
|
29
|
-
"demo": "npm run
|
43
|
+
"build": "rm -rf ./dist && rslib build",
|
44
|
+
"buildAll": "npm run build && cd ../../examples/realtime-console && npm run build",
|
45
|
+
"demo": "npm run start & cd ../../examples/realtime-console && npm run start",
|
30
46
|
"format": "prettier --write .",
|
31
47
|
"lint": "eslint ./ --cache --quiet",
|
32
48
|
"prepublishOnly": "npm run build",
|
33
|
-
"start": "rm -rf dist &&
|
49
|
+
"start": "rm -rf dist && rslib build -w",
|
34
50
|
"test": "vitest",
|
35
51
|
"test:cov": "vitest --coverage --run"
|
36
52
|
},
|
37
53
|
"dependencies": {
|
38
|
-
"@coze/api": "1.0.
|
54
|
+
"@coze/api": "1.0.10",
|
39
55
|
"@volcengine/rtc": "^4.62.1"
|
40
56
|
},
|
41
57
|
"devDependencies": {
|
42
58
|
"@coze-infra/eslint-config": "workspace:*",
|
43
59
|
"@coze-infra/ts-config": "workspace:*",
|
44
60
|
"@coze-infra/vitest-config": "workspace:*",
|
61
|
+
"@rslib/core": "0.0.18",
|
45
62
|
"@swc/core": "^1.3.14",
|
46
63
|
"@types/node": "^20",
|
47
64
|
"@types/uuid": "^9.0.1",
|
package/dist/client.js
DELETED
@@ -1,242 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
3
|
-
if (k2 === undefined) k2 = k;
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
7
|
-
}
|
8
|
-
Object.defineProperty(o, k2, desc);
|
9
|
-
}) : (function(o, m, k, k2) {
|
10
|
-
if (k2 === undefined) k2 = k;
|
11
|
-
o[k2] = m[k];
|
12
|
-
}));
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
15
|
-
}) : function(o, v) {
|
16
|
-
o["default"] = v;
|
17
|
-
});
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
19
|
-
if (mod && mod.__esModule) return mod;
|
20
|
-
var result = {};
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
22
|
-
__setModuleDefault(result, mod);
|
23
|
-
return result;
|
24
|
-
};
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
27
|
-
};
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
29
|
-
exports.EngineClient = void 0;
|
30
|
-
const extension_ainr_1 = __importDefault(require("@volcengine/rtc/extension-ainr"));
|
31
|
-
const rtc_1 = __importStar(require("@volcengine/rtc"));
|
32
|
-
const utils_1 = require("./utils");
|
33
|
-
const event_handler_1 = require("./event-handler");
|
34
|
-
const error_1 = require("./error");
|
35
|
-
class EngineClient extends event_handler_1.RealtimeEventHandler {
|
36
|
-
constructor(appId, debug = false, isTestEnv = false) {
|
37
|
-
super(debug);
|
38
|
-
this.joinUserId = '';
|
39
|
-
this._AIAnsExtension = null;
|
40
|
-
if (isTestEnv) {
|
41
|
-
rtc_1.default.setParameter('ICE_CONFIG_REQUEST_URLS', ['rtc-test.bytedance.com']);
|
42
|
-
}
|
43
|
-
this.engine = rtc_1.default.createEngine(appId);
|
44
|
-
this.handleMessage = this.handleMessage.bind(this);
|
45
|
-
this.handleUserJoin = this.handleUserJoin.bind(this);
|
46
|
-
this.handleUserLeave = this.handleUserLeave.bind(this);
|
47
|
-
this.handleEventError = this.handleEventError.bind(this);
|
48
|
-
// Debug only
|
49
|
-
this.handleLocalAudioPropertiesReport =
|
50
|
-
this.handleLocalAudioPropertiesReport.bind(this);
|
51
|
-
this.handleRemoteAudioPropertiesReport =
|
52
|
-
this.handleRemoteAudioPropertiesReport.bind(this);
|
53
|
-
}
|
54
|
-
bindEngineEvents() {
|
55
|
-
this.engine.on(rtc_1.default.events.onUserMessageReceived, this.handleMessage);
|
56
|
-
this.engine.on(rtc_1.default.events.onUserJoined, this.handleUserJoin);
|
57
|
-
this.engine.on(rtc_1.default.events.onUserLeave, this.handleUserLeave);
|
58
|
-
this.engine.on(rtc_1.default.events.onError, this.handleEventError);
|
59
|
-
if (this._debug) {
|
60
|
-
this.engine.on(rtc_1.default.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
61
|
-
this.engine.on(rtc_1.default.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
|
62
|
-
}
|
63
|
-
}
|
64
|
-
removeEventListener() {
|
65
|
-
this.engine.off(rtc_1.default.events.onUserMessageReceived, this.handleMessage);
|
66
|
-
this.engine.off(rtc_1.default.events.onUserJoined, this.handleUserJoin);
|
67
|
-
this.engine.off(rtc_1.default.events.onUserLeave, this.handleUserLeave);
|
68
|
-
this.engine.off(rtc_1.default.events.onError, this.handleEventError);
|
69
|
-
if (this._debug) {
|
70
|
-
this.engine.off(rtc_1.default.events.onLocalAudioPropertiesReport, this.handleLocalAudioPropertiesReport);
|
71
|
-
this.engine.off(rtc_1.default.events.onRemoteAudioPropertiesReport, this.handleRemoteAudioPropertiesReport);
|
72
|
-
}
|
73
|
-
}
|
74
|
-
handleMessage(event) {
|
75
|
-
try {
|
76
|
-
const message = JSON.parse(event.message);
|
77
|
-
this.dispatch(`server.${message.event_type}`, message);
|
78
|
-
}
|
79
|
-
catch (e) {
|
80
|
-
this.dispatch('client.error', {
|
81
|
-
message: `Failed to parse message: ${event.message}`,
|
82
|
-
error: e,
|
83
|
-
});
|
84
|
-
}
|
85
|
-
}
|
86
|
-
handleEventError(e) {
|
87
|
-
this.dispatch('client.error', e);
|
88
|
-
}
|
89
|
-
handleUserJoin(event) {
|
90
|
-
this.joinUserId = event.userInfo.userId;
|
91
|
-
this.dispatch('server.bot.join', event);
|
92
|
-
}
|
93
|
-
handleUserLeave(event) {
|
94
|
-
this.dispatch('server.bot.leave', event);
|
95
|
-
}
|
96
|
-
async joinRoom(options) {
|
97
|
-
const { token, roomId, uid, audioMutedDefault = false } = options;
|
98
|
-
try {
|
99
|
-
await this.engine.joinRoom(token, roomId, {
|
100
|
-
userId: uid,
|
101
|
-
}, {
|
102
|
-
isAutoPublish: !audioMutedDefault,
|
103
|
-
isAutoSubscribeAudio: true,
|
104
|
-
isAutoSubscribeVideo: false,
|
105
|
-
});
|
106
|
-
}
|
107
|
-
catch (e) {
|
108
|
-
if (e instanceof Error) {
|
109
|
-
throw new error_1.RealtimeAPIError(error_1.RealtimeError.CONNECTION_ERROR, e.message);
|
110
|
-
}
|
111
|
-
throw new error_1.RealtimeAPIError(error_1.RealtimeError.CONNECTION_ERROR, 'Unknown error');
|
112
|
-
}
|
113
|
-
}
|
114
|
-
async setAudioInputDevice(deviceId) {
|
115
|
-
const devices = await (0, utils_1.getAudioDevices)();
|
116
|
-
if (devices.audioInputs.findIndex(i => i.deviceId === deviceId) === -1) {
|
117
|
-
throw new error_1.RealtimeAPIError(error_1.RealtimeError.DEVICE_ACCESS_ERROR, `Audio input device not found: ${deviceId}`);
|
118
|
-
}
|
119
|
-
this.engine.stopAudioCapture();
|
120
|
-
await this.engine.startAudioCapture(deviceId);
|
121
|
-
}
|
122
|
-
async setAudioOutputDevice(deviceId) {
|
123
|
-
const devices = await (0, utils_1.getAudioDevices)();
|
124
|
-
if (devices.audioOutputs.findIndex(i => i.deviceId === deviceId) === -1) {
|
125
|
-
throw new error_1.RealtimeAPIError(error_1.RealtimeError.DEVICE_ACCESS_ERROR, `Audio output device not found: ${deviceId}`);
|
126
|
-
}
|
127
|
-
await this.engine.setAudioPlaybackDevice(deviceId);
|
128
|
-
}
|
129
|
-
async createLocalStream() {
|
130
|
-
const devices = await (0, utils_1.getAudioDevices)();
|
131
|
-
if (!devices.audioInputs.length) {
|
132
|
-
throw new error_1.RealtimeAPIError(error_1.RealtimeError.DEVICE_ACCESS_ERROR, 'Failed to get devices');
|
133
|
-
}
|
134
|
-
await this.engine.startAudioCapture(devices.audioInputs[0].deviceId);
|
135
|
-
}
|
136
|
-
async disconnect() {
|
137
|
-
try {
|
138
|
-
await this.engine.stopAudioCapture();
|
139
|
-
await this.engine.unpublishStream(rtc_1.MediaType.AUDIO);
|
140
|
-
await this.engine.leaveRoom();
|
141
|
-
this.removeEventListener();
|
142
|
-
}
|
143
|
-
catch (e) {
|
144
|
-
this.dispatch('client.error', e);
|
145
|
-
throw e;
|
146
|
-
}
|
147
|
-
}
|
148
|
-
async changeAudioState(isMicOn) {
|
149
|
-
try {
|
150
|
-
if (isMicOn) {
|
151
|
-
await this.engine.publishStream(rtc_1.MediaType.AUDIO);
|
152
|
-
}
|
153
|
-
else {
|
154
|
-
await this.engine.unpublishStream(rtc_1.MediaType.AUDIO);
|
155
|
-
}
|
156
|
-
}
|
157
|
-
catch (e) {
|
158
|
-
this.dispatch('client.error', e);
|
159
|
-
throw e;
|
160
|
-
}
|
161
|
-
}
|
162
|
-
async stop() {
|
163
|
-
try {
|
164
|
-
const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify({
|
165
|
-
id: 'event_1',
|
166
|
-
event_type: 'conversation.chat.cancel',
|
167
|
-
data: {},
|
168
|
-
}));
|
169
|
-
this._log(`interrupt ${this.joinUserId} ${result}`);
|
170
|
-
}
|
171
|
-
catch (e) {
|
172
|
-
this.dispatch('client.error', e);
|
173
|
-
throw e;
|
174
|
-
}
|
175
|
-
}
|
176
|
-
async sendMessage(message) {
|
177
|
-
try {
|
178
|
-
const result = await this.engine.sendUserMessage(this.joinUserId, JSON.stringify(message));
|
179
|
-
this._log(`sendMessage ${this.joinUserId} ${JSON.stringify(message)} ${result}`);
|
180
|
-
}
|
181
|
-
catch (e) {
|
182
|
-
this.dispatch('client.error', e);
|
183
|
-
throw e;
|
184
|
-
}
|
185
|
-
}
|
186
|
-
enableAudioPropertiesReport(config) {
|
187
|
-
this.engine.enableAudioPropertiesReport(config);
|
188
|
-
}
|
189
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
190
|
-
handleLocalAudioPropertiesReport(event) {
|
191
|
-
var _a, _b;
|
192
|
-
if (this._debug && ((_b = (_a = event[0]) === null || _a === void 0 ? void 0 : _a.audioPropertiesInfo) === null || _b === void 0 ? void 0 : _b.linearVolume) > 0) {
|
193
|
-
console.log('handleLocalAudioPropertiesReport', event);
|
194
|
-
}
|
195
|
-
}
|
196
|
-
handleRemoteAudioPropertiesReport(event) {
|
197
|
-
if (this._debug) {
|
198
|
-
console.log('handleRemoteAudioPropertiesReport', event);
|
199
|
-
}
|
200
|
-
}
|
201
|
-
async enableAudioNoiseReduction() {
|
202
|
-
var _a;
|
203
|
-
await ((_a = this.engine) === null || _a === void 0 ? void 0 : _a.setAudioCaptureConfig({
|
204
|
-
noiseSuppression: true,
|
205
|
-
echoCancellation: true,
|
206
|
-
autoGainControl: true,
|
207
|
-
}));
|
208
|
-
}
|
209
|
-
async initAIAnsExtension() {
|
210
|
-
const AIAnsExtension = new extension_ainr_1.default();
|
211
|
-
await this.engine.registerExtension(AIAnsExtension);
|
212
|
-
this._AIAnsExtension = AIAnsExtension;
|
213
|
-
}
|
214
|
-
changeAIAnsExtension(enable) {
|
215
|
-
var _a, _b;
|
216
|
-
if (enable) {
|
217
|
-
(_a = this._AIAnsExtension) === null || _a === void 0 ? void 0 : _a.enable();
|
218
|
-
}
|
219
|
-
else {
|
220
|
-
(_b = this._AIAnsExtension) === null || _b === void 0 ? void 0 : _b.disable();
|
221
|
-
}
|
222
|
-
}
|
223
|
-
async startAudioPlaybackDeviceTest() {
|
224
|
-
try {
|
225
|
-
await this.engine.startAudioPlaybackDeviceTest('audio-test.wav', 200);
|
226
|
-
}
|
227
|
-
catch (e) {
|
228
|
-
this.dispatch('client.error', e);
|
229
|
-
throw e;
|
230
|
-
}
|
231
|
-
}
|
232
|
-
stopAudioPlaybackDeviceTest() {
|
233
|
-
try {
|
234
|
-
this.engine.stopAudioPlaybackDeviceTest();
|
235
|
-
}
|
236
|
-
catch (e) {
|
237
|
-
this.dispatch('client.error', e);
|
238
|
-
throw e;
|
239
|
-
}
|
240
|
-
}
|
241
|
-
}
|
242
|
-
exports.EngineClient = EngineClient;
|
package/dist/error.js
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.RealtimeAPIError = exports.ErrorMessages = exports.RealtimeError = void 0;
|
4
|
-
var RealtimeError;
|
5
|
-
(function (RealtimeError) {
|
6
|
-
RealtimeError["DEVICE_ACCESS_ERROR"] = "DEVICE_ACCESS_ERROR";
|
7
|
-
RealtimeError["STREAM_CREATION_ERROR"] = "STREAM_CREATION_ERROR";
|
8
|
-
RealtimeError["CONNECTION_ERROR"] = "CONNECTION_ERROR";
|
9
|
-
RealtimeError["DISCONNECTION_ERROR"] = "DISCONNECTION_ERROR";
|
10
|
-
RealtimeError["INTERRUPT_ERROR"] = "INTERRUPT_ERROR";
|
11
|
-
RealtimeError["EVENT_HANDLER_ERROR"] = "EVENT_HANDLER_ERROR";
|
12
|
-
RealtimeError["PERMISSION_DENIED"] = "PERMISSION_DENIED";
|
13
|
-
RealtimeError["NETWORK_ERROR"] = "NETWORK_ERROR";
|
14
|
-
RealtimeError["INVALID_STATE"] = "INVALID_STATE";
|
15
|
-
RealtimeError["CREATE_ROOM_ERROR"] = "CREATE_ROOM_ERROR";
|
16
|
-
})(RealtimeError || (exports.RealtimeError = RealtimeError = {}));
|
17
|
-
exports.ErrorMessages = {
|
18
|
-
[RealtimeError.DEVICE_ACCESS_ERROR]: 'Failed to get devices',
|
19
|
-
[RealtimeError.STREAM_CREATION_ERROR]: 'Failed to create local stream',
|
20
|
-
[RealtimeError.CONNECTION_ERROR]: 'Failed to connect',
|
21
|
-
[RealtimeError.DISCONNECTION_ERROR]: 'Failed to disconnect',
|
22
|
-
[RealtimeError.INTERRUPT_ERROR]: 'Failed to interrupt',
|
23
|
-
[RealtimeError.EVENT_HANDLER_ERROR]: 'Event handler not found',
|
24
|
-
[RealtimeError.PERMISSION_DENIED]: 'Permission denied for requested operation',
|
25
|
-
[RealtimeError.NETWORK_ERROR]: 'Network connection error occurred',
|
26
|
-
[RealtimeError.INVALID_STATE]: 'Operation invalid in current state',
|
27
|
-
[RealtimeError.CREATE_ROOM_ERROR]: 'Failed to create room',
|
28
|
-
};
|
29
|
-
class RealtimeAPIError extends Error {
|
30
|
-
constructor(code, message) {
|
31
|
-
super(`[${code}] ${message}`);
|
32
|
-
this.code = code;
|
33
|
-
this.name = 'RealtimeAPIError';
|
34
|
-
}
|
35
|
-
}
|
36
|
-
exports.RealtimeAPIError = RealtimeAPIError;
|
package/dist/event-handler.js
DELETED
@@ -1,126 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.RealtimeEventHandler = exports.EventNames = void 0;
|
4
|
-
const error_1 = require("./error");
|
5
|
-
var EventNames;
|
6
|
-
(function (EventNames) {
|
7
|
-
/**
|
8
|
-
* en: All events
|
9
|
-
* zh: 所有事件
|
10
|
-
*/
|
11
|
-
EventNames["ALL"] = "realtime.event";
|
12
|
-
/**
|
13
|
-
* en: All client events
|
14
|
-
* zh: 所有客户端事件
|
15
|
-
*/
|
16
|
-
EventNames["ALL_CLIENT"] = "client.*";
|
17
|
-
/**
|
18
|
-
* en: All server events
|
19
|
-
* zh: 所有服务端事件
|
20
|
-
*/
|
21
|
-
EventNames["ALL_SERVER"] = "server.*";
|
22
|
-
/**
|
23
|
-
* en: Client connected
|
24
|
-
* zh: 客户端连接
|
25
|
-
*/
|
26
|
-
EventNames["CONNECTED"] = "client.connected";
|
27
|
-
/**
|
28
|
-
* en: Client interrupted
|
29
|
-
* zh: 客户端中断
|
30
|
-
*/
|
31
|
-
EventNames["INTERRUPTED"] = "client.interrupted";
|
32
|
-
/**
|
33
|
-
* en: Client disconnected
|
34
|
-
* zh: 客户端断开
|
35
|
-
*/
|
36
|
-
EventNames["DISCONNECTED"] = "client.disconnected";
|
37
|
-
/**
|
38
|
-
* en: Client audio unmuted
|
39
|
-
* zh: 客户端音频未静音
|
40
|
-
*/
|
41
|
-
EventNames["AUDIO_UNMUTED"] = "client.audio.unmuted";
|
42
|
-
/**
|
43
|
-
* en: Client audio muted
|
44
|
-
* zh: 客户端音频静音
|
45
|
-
*/
|
46
|
-
EventNames["AUDIO_MUTED"] = "client.audio.muted";
|
47
|
-
/**
|
48
|
-
* en: Client error
|
49
|
-
* zh: 客户端错误
|
50
|
-
*/
|
51
|
-
EventNames["ERROR"] = "client.error";
|
52
|
-
/**
|
53
|
-
* en: Audio noise reduction enabled
|
54
|
-
* zh: 抑制平稳噪声
|
55
|
-
*/
|
56
|
-
EventNames["SUPPRESS_STATIONARY_NOISE"] = "client.suppress.stationary.noise";
|
57
|
-
/**
|
58
|
-
* en: Suppress non-stationary noise
|
59
|
-
* zh: 抑制非平稳噪声
|
60
|
-
*/
|
61
|
-
EventNames["SUPPRESS_NON_STATIONARY_NOISE"] = "client.suppress.non.stationary.noise";
|
62
|
-
/**
|
63
|
-
* en: Audio input device changed
|
64
|
-
* zh: 音频输入设备改变
|
65
|
-
*/
|
66
|
-
EventNames["AUDIO_INPUT_DEVICE_CHANGED"] = "client.input.device.changed";
|
67
|
-
/**
|
68
|
-
* en: Audio output device changed
|
69
|
-
* zh: 音频输出设备改变
|
70
|
-
*/
|
71
|
-
EventNames["AUDIO_OUTPUT_DEVICE_CHANGED"] = "client.output.device.changed";
|
72
|
-
})(EventNames || (exports.EventNames = EventNames = {}));
|
73
|
-
class RealtimeEventHandler {
|
74
|
-
constructor(debug = false) {
|
75
|
-
this.eventHandlers = {};
|
76
|
-
this._debug = debug;
|
77
|
-
}
|
78
|
-
clearEventHandlers() {
|
79
|
-
this.eventHandlers = {};
|
80
|
-
}
|
81
|
-
on(eventName, callback) {
|
82
|
-
this._log(`on ${eventName} event`);
|
83
|
-
this.eventHandlers[eventName] = this.eventHandlers[eventName] || [];
|
84
|
-
this.eventHandlers[eventName].push(callback);
|
85
|
-
return callback;
|
86
|
-
}
|
87
|
-
off(eventName, callback) {
|
88
|
-
this._log(`off ${eventName} event`);
|
89
|
-
const handlers = this.eventHandlers[eventName] || [];
|
90
|
-
if (callback) {
|
91
|
-
const index = handlers.indexOf(callback);
|
92
|
-
if (index === -1) {
|
93
|
-
throw new error_1.RealtimeAPIError(error_1.RealtimeError.EVENT_HANDLER_ERROR, `Could not turn off specified event listener for "${eventName}": not found as a listener`);
|
94
|
-
}
|
95
|
-
handlers.splice(index, 1);
|
96
|
-
}
|
97
|
-
else {
|
98
|
-
delete this.eventHandlers[eventName];
|
99
|
-
}
|
100
|
-
}
|
101
|
-
// eslint-disable-next-line max-params
|
102
|
-
_dispatchToHandlers(eventName, event, handlers, prefix) {
|
103
|
-
for (const handler of handlers) {
|
104
|
-
if (!prefix || eventName.startsWith(prefix)) {
|
105
|
-
handler(eventName, event);
|
106
|
-
}
|
107
|
-
}
|
108
|
-
}
|
109
|
-
dispatch(eventName, event) {
|
110
|
-
this._log(`dispatch ${eventName} event`);
|
111
|
-
const handlers = (this.eventHandlers[eventName] || []).slice();
|
112
|
-
this._dispatchToHandlers(eventName, event, handlers);
|
113
|
-
const allHandlers = (this.eventHandlers[EventNames.ALL] || []).slice();
|
114
|
-
this._dispatchToHandlers(eventName, event, allHandlers);
|
115
|
-
const allClientHandlers = (this.eventHandlers[EventNames.ALL_CLIENT] || []).slice();
|
116
|
-
this._dispatchToHandlers(eventName, event, allClientHandlers, 'client.');
|
117
|
-
const allServerHandlers = (this.eventHandlers[EventNames.ALL_SERVER] || []).slice();
|
118
|
-
this._dispatchToHandlers(eventName, event, allServerHandlers, 'server.');
|
119
|
-
}
|
120
|
-
_log(message) {
|
121
|
-
if (this._debug) {
|
122
|
-
console.log(`[RealtimeClient] ${message}`);
|
123
|
-
}
|
124
|
-
}
|
125
|
-
}
|
126
|
-
exports.RealtimeEventHandler = RealtimeEventHandler;
|