@koi-design/callkit 2.0.4 → 2.0.5
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/index.d.ts +32 -8
- package/dist/index.global.js +122 -56
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +122 -56
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +122 -56
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -612,13 +612,13 @@ var Call = class {
|
|
|
612
612
|
// package.json
|
|
613
613
|
var package_default = {
|
|
614
614
|
name: "@koi-design/callkit",
|
|
615
|
-
version: "2.0.
|
|
615
|
+
version: "2.0.5",
|
|
616
616
|
description: "callkit",
|
|
617
617
|
author: "koi",
|
|
618
618
|
license: "ISC",
|
|
619
619
|
scripts: {
|
|
620
620
|
build: "tsup",
|
|
621
|
-
"build:
|
|
621
|
+
"build:w": "tsup --watch",
|
|
622
622
|
dev: "vite",
|
|
623
623
|
lint: "eslint -c ../../.eslintrc.js --ext .jsx,.js,.tsx,.ts ./package --fix",
|
|
624
624
|
release: "tsup && node scripts/pkg.js"
|
|
@@ -1899,23 +1899,41 @@ var RECONNECT_CONFIG = {
|
|
|
1899
1899
|
var Socket = class {
|
|
1900
1900
|
callKit;
|
|
1901
1901
|
ws;
|
|
1902
|
-
socketConfig;
|
|
1903
1902
|
lastPingTime = void 0;
|
|
1904
|
-
isConnected = false;
|
|
1905
1903
|
pingTimer;
|
|
1906
|
-
|
|
1907
|
-
|
|
1904
|
+
/**
|
|
1905
|
+
* @description reconnect timer
|
|
1906
|
+
*/
|
|
1908
1907
|
reconnectTimer;
|
|
1909
|
-
|
|
1908
|
+
/**
|
|
1909
|
+
* @description reconnect attempts
|
|
1910
|
+
*/
|
|
1910
1911
|
reconnectAttempts = 0;
|
|
1911
|
-
|
|
1912
|
+
/**
|
|
1913
|
+
* @description connect auth state
|
|
1914
|
+
* @default {
|
|
1915
|
+
* satrtConfirm: false,
|
|
1916
|
+
* isConnected: false,
|
|
1917
|
+
* isReconnecting: false,
|
|
1918
|
+
* isAuthenticated: false,
|
|
1919
|
+
* isError: false
|
|
1920
|
+
* }
|
|
1921
|
+
*/
|
|
1922
|
+
connectAuthState = {
|
|
1923
|
+
satrtConfirm: false,
|
|
1924
|
+
isConnected: false,
|
|
1925
|
+
isReconnecting: false,
|
|
1926
|
+
isAuthenticated: false,
|
|
1927
|
+
isError: false
|
|
1928
|
+
};
|
|
1929
|
+
get satrtConfirm() {
|
|
1930
|
+
return this.connectAuthState.satrtConfirm;
|
|
1931
|
+
}
|
|
1932
|
+
get isError() {
|
|
1933
|
+
return this.connectAuthState.isError;
|
|
1934
|
+
}
|
|
1912
1935
|
constructor(callKit) {
|
|
1913
1936
|
this.callKit = callKit;
|
|
1914
|
-
const { reconnect } = this.callKit.config.getConfig();
|
|
1915
|
-
this.socketConfig = {
|
|
1916
|
-
...RECONNECT_CONFIG,
|
|
1917
|
-
...reconnect
|
|
1918
|
-
};
|
|
1919
1937
|
}
|
|
1920
1938
|
init() {
|
|
1921
1939
|
const { socket } = this.callKit.config.getConfig();
|
|
@@ -1928,19 +1946,29 @@ var Socket = class {
|
|
|
1928
1946
|
});
|
|
1929
1947
|
this.connect(socket);
|
|
1930
1948
|
}
|
|
1949
|
+
getSocketConfig() {
|
|
1950
|
+
const { reconnect } = this.callKit.config.getConfig();
|
|
1951
|
+
return {
|
|
1952
|
+
...RECONNECT_CONFIG,
|
|
1953
|
+
...reconnect
|
|
1954
|
+
};
|
|
1955
|
+
}
|
|
1956
|
+
setConnectAuthState(key, value) {
|
|
1957
|
+
if (this.connectAuthState[key] === value)
|
|
1958
|
+
return;
|
|
1959
|
+
this.connectAuthState[key] = value;
|
|
1960
|
+
}
|
|
1931
1961
|
handleDisconnect() {
|
|
1932
|
-
this.isConnected
|
|
1933
|
-
|
|
1934
|
-
|
|
1962
|
+
this.setConnectAuthState("isConnected", false);
|
|
1963
|
+
const { enabled } = this.getSocketConfig();
|
|
1964
|
+
if (!this.callKit.config.isLogin() || !enabled) {
|
|
1965
|
+
this.callKit.reset();
|
|
1935
1966
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
1936
1967
|
event: "INCALL_NOT_CONNECTED"
|
|
1937
1968
|
});
|
|
1938
1969
|
return;
|
|
1939
1970
|
}
|
|
1940
|
-
if (this.
|
|
1941
|
-
return;
|
|
1942
|
-
}
|
|
1943
|
-
if (this.isReconnecting) {
|
|
1971
|
+
if (this.connectAuthState.isReconnecting) {
|
|
1944
1972
|
return;
|
|
1945
1973
|
}
|
|
1946
1974
|
this.attemptReconnect();
|
|
@@ -1961,7 +1989,7 @@ var Socket = class {
|
|
|
1961
1989
|
});
|
|
1962
1990
|
}
|
|
1963
1991
|
this.ws = void 0;
|
|
1964
|
-
this.isConnected
|
|
1992
|
+
this.setConnectAuthState("isConnected", false);
|
|
1965
1993
|
}
|
|
1966
1994
|
connect(socketUrl) {
|
|
1967
1995
|
if (this.ws) {
|
|
@@ -1979,11 +2007,11 @@ var Socket = class {
|
|
|
1979
2007
|
type: "INCALL",
|
|
1980
2008
|
content: { ev }
|
|
1981
2009
|
});
|
|
1982
|
-
this.
|
|
1983
|
-
this.isConnected = true;
|
|
2010
|
+
this.setConnectAuthState("isConnected", true);
|
|
1984
2011
|
this.lastPingTime = Date.now();
|
|
1985
2012
|
this.checkPing();
|
|
1986
|
-
if (this.isReconnecting) {
|
|
2013
|
+
if (this.connectAuthState.isReconnecting) {
|
|
2014
|
+
this.setConnectAuthState("isReconnecting", false);
|
|
1987
2015
|
this.callKit.logger.info("reconnect success", {
|
|
1988
2016
|
caller: "Socket.onOpen",
|
|
1989
2017
|
type: "INCALL",
|
|
@@ -1996,10 +2024,15 @@ var Socket = class {
|
|
|
1996
2024
|
event: "INCALL_RECONNECT_SUCCESS"
|
|
1997
2025
|
});
|
|
1998
2026
|
}
|
|
1999
|
-
this.resetReconnectState();
|
|
2000
2027
|
}
|
|
2001
|
-
|
|
2002
|
-
this.
|
|
2028
|
+
resetConnectState() {
|
|
2029
|
+
this.connectAuthState = {
|
|
2030
|
+
satrtConfirm: false,
|
|
2031
|
+
isConnected: false,
|
|
2032
|
+
isReconnecting: false,
|
|
2033
|
+
isAuthenticated: false,
|
|
2034
|
+
isError: false
|
|
2035
|
+
};
|
|
2003
2036
|
this.reconnectAttempts = 0;
|
|
2004
2037
|
if (this.reconnectTimer) {
|
|
2005
2038
|
clearTimeout(this.reconnectTimer);
|
|
@@ -2015,7 +2048,6 @@ var Socket = class {
|
|
|
2015
2048
|
this.handleDisconnect();
|
|
2016
2049
|
}
|
|
2017
2050
|
onError(ev) {
|
|
2018
|
-
this.socketError = true;
|
|
2019
2051
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2020
2052
|
event: "INCALL_CONNECT_ERROR",
|
|
2021
2053
|
err: ev
|
|
@@ -2065,6 +2097,20 @@ var Socket = class {
|
|
|
2065
2097
|
this.confirmAck(data);
|
|
2066
2098
|
if (data.event === SocketReceiveEvent.PONG) {
|
|
2067
2099
|
this.lastPingTime = Date.now();
|
|
2100
|
+
this.setConnectAuthState("isAuthenticated", true);
|
|
2101
|
+
this.callKit.logger.info("socket onMessage pong Authenticated", {
|
|
2102
|
+
caller: "Socket.onMessage",
|
|
2103
|
+
type: "INCALL",
|
|
2104
|
+
content: {
|
|
2105
|
+
data: content,
|
|
2106
|
+
event: SocketReceiveEvent.PONG,
|
|
2107
|
+
isAuthenticated: true
|
|
2108
|
+
}
|
|
2109
|
+
});
|
|
2110
|
+
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2111
|
+
event: "INCALL_AUTHENTICATED",
|
|
2112
|
+
isAuthenticated: true
|
|
2113
|
+
});
|
|
2068
2114
|
return;
|
|
2069
2115
|
}
|
|
2070
2116
|
if (data.event === SocketReceiveEvent.START_CONFIRM) {
|
|
@@ -2076,7 +2122,7 @@ var Socket = class {
|
|
|
2076
2122
|
event: SocketReceiveEvent.START_CONFIRM
|
|
2077
2123
|
}
|
|
2078
2124
|
});
|
|
2079
|
-
this.satrtConfirm
|
|
2125
|
+
this.setConnectAuthState("satrtConfirm", true);
|
|
2080
2126
|
}
|
|
2081
2127
|
if (data.event === SocketReceiveEvent.CALL_SUCCESS) {
|
|
2082
2128
|
this.callKit.logger.info("call success", {
|
|
@@ -2190,12 +2236,10 @@ var Socket = class {
|
|
|
2190
2236
|
event: SocketReceiveEvent.CLOSE
|
|
2191
2237
|
}
|
|
2192
2238
|
});
|
|
2193
|
-
this.send(SocketSendEvent.END, {
|
|
2194
|
-
agentId: userInfo.agentId
|
|
2195
|
-
});
|
|
2239
|
+
this.send(SocketSendEvent.END, { agentId: userInfo.agentId });
|
|
2196
2240
|
}
|
|
2197
2241
|
if (data.event === SocketReceiveEvent.ERROR) {
|
|
2198
|
-
this.
|
|
2242
|
+
this.setConnectAuthState("isError", true);
|
|
2199
2243
|
this.callKit.reset();
|
|
2200
2244
|
this.callKit.logger.error(data.msg, {
|
|
2201
2245
|
caller: `Socket.onMessage:${data.event}`,
|
|
@@ -2207,7 +2251,7 @@ var Socket = class {
|
|
|
2207
2251
|
});
|
|
2208
2252
|
}
|
|
2209
2253
|
if (data.event === SocketReceiveEvent.SESSION_ERROR) {
|
|
2210
|
-
this.
|
|
2254
|
+
this.setConnectAuthState("isError", true);
|
|
2211
2255
|
this.callKit.reset();
|
|
2212
2256
|
this.callKit.logger.error(data.msg, {
|
|
2213
2257
|
caller: `Socket.onMessage:${data.event}`,
|
|
@@ -2234,7 +2278,7 @@ var Socket = class {
|
|
|
2234
2278
|
this.callKit.trigger(KitEvent.SERVER_SOCKET_EVENT, data);
|
|
2235
2279
|
}
|
|
2236
2280
|
send(event, message) {
|
|
2237
|
-
if (!this.isConnected) {
|
|
2281
|
+
if (!this.connectAuthState.isConnected) {
|
|
2238
2282
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2239
2283
|
event: "INCALL_NOT_CONNECTED"
|
|
2240
2284
|
});
|
|
@@ -2293,7 +2337,7 @@ var Socket = class {
|
|
|
2293
2337
|
}
|
|
2294
2338
|
}
|
|
2295
2339
|
ping() {
|
|
2296
|
-
if (!this.isConnected)
|
|
2340
|
+
if (!this.connectAuthState.isConnected)
|
|
2297
2341
|
return;
|
|
2298
2342
|
this.send(SocketSendEvent.PING);
|
|
2299
2343
|
this.callKit.logger.info(`socket ping`, {
|
|
@@ -2304,12 +2348,12 @@ var Socket = class {
|
|
|
2304
2348
|
}
|
|
2305
2349
|
});
|
|
2306
2350
|
const now = Date.now();
|
|
2307
|
-
const { pingInterval, pingTimeout } = this.
|
|
2351
|
+
const { pingInterval, pingTimeout } = this.getSocketConfig();
|
|
2308
2352
|
if (now - this.lastPingTime > pingInterval + pingTimeout) {
|
|
2309
|
-
if (this.ws && this.isConnected) {
|
|
2353
|
+
if (this.ws && this.connectAuthState.isConnected) {
|
|
2310
2354
|
this.ws.close(4001, "ping timeout");
|
|
2311
2355
|
} else {
|
|
2312
|
-
this.reset();
|
|
2356
|
+
this.callKit.reset();
|
|
2313
2357
|
}
|
|
2314
2358
|
this.callKit.logger.error("socket ping timeout", {
|
|
2315
2359
|
caller: "Socket.ping",
|
|
@@ -2325,21 +2369,29 @@ var Socket = class {
|
|
|
2325
2369
|
clearInterval(this.pingTimer);
|
|
2326
2370
|
}
|
|
2327
2371
|
this.ping();
|
|
2372
|
+
const { pingInterval } = this.getSocketConfig();
|
|
2328
2373
|
this.pingTimer = setInterval(() => {
|
|
2329
2374
|
this.ping();
|
|
2330
|
-
},
|
|
2375
|
+
}, pingInterval);
|
|
2331
2376
|
}
|
|
2332
2377
|
/**
|
|
2333
2378
|
* reset socket connection and all states
|
|
2334
2379
|
*/
|
|
2335
|
-
async reset() {
|
|
2380
|
+
async reset(config) {
|
|
2381
|
+
const { focus = false } = config || {};
|
|
2336
2382
|
if (this.pingTimer) {
|
|
2337
2383
|
clearInterval(this.pingTimer);
|
|
2338
2384
|
this.pingTimer = void 0;
|
|
2339
2385
|
}
|
|
2340
|
-
|
|
2386
|
+
if (focus) {
|
|
2387
|
+
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2388
|
+
event: "INCALL_RESET"
|
|
2389
|
+
});
|
|
2390
|
+
this.resetConnectState();
|
|
2391
|
+
this.setConnectAuthState("isConnected", false);
|
|
2392
|
+
}
|
|
2341
2393
|
this.lastPingTime = void 0;
|
|
2342
|
-
this.satrtConfirm
|
|
2394
|
+
this.setConnectAuthState("satrtConfirm", false);
|
|
2343
2395
|
this.clearWebSocket();
|
|
2344
2396
|
}
|
|
2345
2397
|
attemptReconnect() {
|
|
@@ -2347,11 +2399,12 @@ var Socket = class {
|
|
|
2347
2399
|
clearTimeout(this.reconnectTimer);
|
|
2348
2400
|
this.reconnectTimer = void 0;
|
|
2349
2401
|
}
|
|
2350
|
-
|
|
2402
|
+
const { maxAttempts } = this.getSocketConfig();
|
|
2403
|
+
if (this.reconnectAttempts >= maxAttempts) {
|
|
2351
2404
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2352
2405
|
event: "INCALL_RECONNECT_ERROR"
|
|
2353
2406
|
});
|
|
2354
|
-
this.reset();
|
|
2407
|
+
this.callKit.reset();
|
|
2355
2408
|
this.callKit.logger.error("Maximum reconnection attempts reached", {
|
|
2356
2409
|
caller: "Socket.attemptReconnect",
|
|
2357
2410
|
type: "INCALL",
|
|
@@ -2367,17 +2420,17 @@ var Socket = class {
|
|
|
2367
2420
|
event: "INCALL_RECONNECT_START"
|
|
2368
2421
|
});
|
|
2369
2422
|
}
|
|
2370
|
-
this.isReconnecting
|
|
2423
|
+
this.setConnectAuthState("isReconnecting", true);
|
|
2371
2424
|
this.reconnectAttempts += 1;
|
|
2372
|
-
const { delay } = this.
|
|
2425
|
+
const { delay } = this.getSocketConfig();
|
|
2373
2426
|
this.callKit.logger.info(
|
|
2374
|
-
`Preparing reconnection attempt ${this.reconnectAttempts}/${
|
|
2427
|
+
`Preparing reconnection attempt ${this.reconnectAttempts}/${maxAttempts}, delay: ${delay}ms`,
|
|
2375
2428
|
{
|
|
2376
2429
|
caller: "Socket.attemptReconnect",
|
|
2377
2430
|
type: "INCALL",
|
|
2378
2431
|
content: {
|
|
2379
2432
|
reconnectAttempts: this.reconnectAttempts,
|
|
2380
|
-
maxAttempts
|
|
2433
|
+
maxAttempts,
|
|
2381
2434
|
delay
|
|
2382
2435
|
}
|
|
2383
2436
|
}
|
|
@@ -2452,6 +2505,13 @@ var CallKit = class {
|
|
|
2452
2505
|
encryptionPassword
|
|
2453
2506
|
}
|
|
2454
2507
|
});
|
|
2508
|
+
if (this.socket.isError) {
|
|
2509
|
+
this.logger.warn("socket is error", {
|
|
2510
|
+
caller: "CallKit.login",
|
|
2511
|
+
content: { username, password, extra, socketError: this.socket.isError }
|
|
2512
|
+
});
|
|
2513
|
+
return;
|
|
2514
|
+
}
|
|
2455
2515
|
try {
|
|
2456
2516
|
const user = await this.api.login({
|
|
2457
2517
|
userName: username,
|
|
@@ -2503,7 +2563,7 @@ var CallKit = class {
|
|
|
2503
2563
|
try {
|
|
2504
2564
|
await this.api.loginOut({ sessionId });
|
|
2505
2565
|
} catch (error) {
|
|
2506
|
-
this.logger.
|
|
2566
|
+
this.logger.warn(error, {
|
|
2507
2567
|
caller: "CallKit.logout",
|
|
2508
2568
|
content: {
|
|
2509
2569
|
errCode: ErrorCode.API_USER_LOGOUT_ERROR
|
|
@@ -2579,9 +2639,6 @@ var CallKit = class {
|
|
|
2579
2639
|
});
|
|
2580
2640
|
this.connect.unregister();
|
|
2581
2641
|
}
|
|
2582
|
-
async stop() {
|
|
2583
|
-
await this.connect.stop();
|
|
2584
|
-
}
|
|
2585
2642
|
async hangup() {
|
|
2586
2643
|
if (!this.config.check())
|
|
2587
2644
|
return;
|
|
@@ -2664,11 +2721,13 @@ var CallKit = class {
|
|
|
2664
2721
|
userStatus: status
|
|
2665
2722
|
});
|
|
2666
2723
|
}
|
|
2667
|
-
async
|
|
2724
|
+
async _reset(config) {
|
|
2725
|
+
const { focus = false } = config || {};
|
|
2668
2726
|
this.logger.info("reset", {
|
|
2669
2727
|
caller: "CallKit.reset",
|
|
2670
2728
|
content: {
|
|
2671
|
-
connectStatus: this.connect.connectStatus
|
|
2729
|
+
connectStatus: this.connect.connectStatus,
|
|
2730
|
+
focus
|
|
2672
2731
|
}
|
|
2673
2732
|
});
|
|
2674
2733
|
if (this.connect.isCalling()) {
|
|
@@ -2679,7 +2738,14 @@ var CallKit = class {
|
|
|
2679
2738
|
await this.logout({ isReset: false });
|
|
2680
2739
|
}
|
|
2681
2740
|
await this.config.reset();
|
|
2682
|
-
await this.socket.reset();
|
|
2741
|
+
await this.socket.reset({ focus });
|
|
2742
|
+
}
|
|
2743
|
+
/**
|
|
2744
|
+
* reset callkit
|
|
2745
|
+
* @description recover the callkit to the initial state
|
|
2746
|
+
*/
|
|
2747
|
+
async reset() {
|
|
2748
|
+
await this._reset({ focus: true });
|
|
2683
2749
|
}
|
|
2684
2750
|
on(event, callback) {
|
|
2685
2751
|
this.listener.push({
|