@koi-design/callkit 2.0.4 → 2.0.5-beta.2
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 +70 -17
- package/dist/index.global.js +191 -207
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +191 -207
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +191 -207
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -27,6 +27,10 @@ var Api = class {
|
|
|
27
27
|
isLoginOuting = false;
|
|
28
28
|
async login(params) {
|
|
29
29
|
if (this.isLogining) {
|
|
30
|
+
this.callKit.logger.info("login is already in progress cancel", {
|
|
31
|
+
caller: "Api.login",
|
|
32
|
+
content: { userName: params.userName }
|
|
33
|
+
});
|
|
30
34
|
return;
|
|
31
35
|
}
|
|
32
36
|
this.isLogining = true;
|
|
@@ -36,15 +40,17 @@ var Api = class {
|
|
|
36
40
|
method: "post",
|
|
37
41
|
data: params
|
|
38
42
|
});
|
|
39
|
-
this.isLogining = false;
|
|
40
43
|
return res;
|
|
41
|
-
}
|
|
44
|
+
} finally {
|
|
42
45
|
this.isLogining = false;
|
|
43
|
-
throw error;
|
|
44
46
|
}
|
|
45
47
|
}
|
|
46
48
|
async loginOut(params) {
|
|
47
49
|
if (this.isLoginOuting) {
|
|
50
|
+
this.callKit.logger.info("loginOut is already in progress cancel", {
|
|
51
|
+
caller: "Api.loginOut",
|
|
52
|
+
content: { sessionId: params.sessionId }
|
|
53
|
+
});
|
|
48
54
|
return;
|
|
49
55
|
}
|
|
50
56
|
this.isLoginOuting = true;
|
|
@@ -54,11 +60,9 @@ var Api = class {
|
|
|
54
60
|
method: "post",
|
|
55
61
|
data: params
|
|
56
62
|
});
|
|
57
|
-
this.isLoginOuting = false;
|
|
58
63
|
return res;
|
|
59
|
-
}
|
|
64
|
+
} finally {
|
|
60
65
|
this.isLoginOuting = false;
|
|
61
|
-
throw error;
|
|
62
66
|
}
|
|
63
67
|
}
|
|
64
68
|
async trackLogs(log) {
|
|
@@ -470,6 +474,18 @@ var trackLogsDefaultConfig = {
|
|
|
470
474
|
interval: 5e3,
|
|
471
475
|
maxSize: 8192
|
|
472
476
|
};
|
|
477
|
+
var SOCKET_RECONNECT_CONFIG = {
|
|
478
|
+
enabled: true,
|
|
479
|
+
maxAttempts: 3,
|
|
480
|
+
delay: 1e3,
|
|
481
|
+
pingInterval: 3e4,
|
|
482
|
+
pingTimeout: 5e3
|
|
483
|
+
};
|
|
484
|
+
var SIP_RECONNECT_CONFIG = {
|
|
485
|
+
enabled: true,
|
|
486
|
+
maxAttempts: 3,
|
|
487
|
+
delay: 1e3
|
|
488
|
+
};
|
|
473
489
|
|
|
474
490
|
// core/call.ts
|
|
475
491
|
var Call = class {
|
|
@@ -483,14 +499,14 @@ var Call = class {
|
|
|
483
499
|
this.callKit.logger.info("callStart", {
|
|
484
500
|
caller: "Call.callStart",
|
|
485
501
|
content: {
|
|
486
|
-
startConfirm: this.callKit.socket.
|
|
502
|
+
startConfirm: this.callKit.socket.startConfirm
|
|
487
503
|
}
|
|
488
504
|
});
|
|
489
|
-
if (!this.callKit.socket.
|
|
505
|
+
if (!this.callKit.socket.startConfirm) {
|
|
490
506
|
this.callKit.logger.warn("server not confirm start", {
|
|
491
507
|
caller: "Call.callStart",
|
|
492
508
|
content: {
|
|
493
|
-
startConfirm: this.callKit.socket.
|
|
509
|
+
startConfirm: this.callKit.socket.startConfirm
|
|
494
510
|
}
|
|
495
511
|
});
|
|
496
512
|
return;
|
|
@@ -612,14 +628,14 @@ var Call = class {
|
|
|
612
628
|
// package.json
|
|
613
629
|
var package_default = {
|
|
614
630
|
name: "@koi-design/callkit",
|
|
615
|
-
version: "2.0.
|
|
631
|
+
version: "2.0.5-beta.2",
|
|
616
632
|
description: "callkit",
|
|
617
633
|
author: "koi",
|
|
618
634
|
license: "ISC",
|
|
619
635
|
scripts: {
|
|
620
636
|
build: "tsup",
|
|
621
|
-
|
|
622
|
-
dev: "
|
|
637
|
+
sraet: "vite",
|
|
638
|
+
dev: "tsup --watch",
|
|
623
639
|
lint: "eslint -c ../../.eslintrc.js --ext .jsx,.js,.tsx,.ts ./package --fix",
|
|
624
640
|
release: "tsup && node scripts/pkg.js"
|
|
625
641
|
},
|
|
@@ -676,6 +692,10 @@ var Config = class {
|
|
|
676
692
|
audioRef: void 0,
|
|
677
693
|
constrains: constrainsDefault,
|
|
678
694
|
socket: "",
|
|
695
|
+
reconnect: {
|
|
696
|
+
sip: SIP_RECONNECT_CONFIG,
|
|
697
|
+
incall: SOCKET_RECONNECT_CONFIG
|
|
698
|
+
},
|
|
679
699
|
userInfo: {
|
|
680
700
|
wsUrl: "",
|
|
681
701
|
logGather: false,
|
|
@@ -760,7 +780,16 @@ var Config = class {
|
|
|
760
780
|
return true;
|
|
761
781
|
}
|
|
762
782
|
getTrackLogsConfig() {
|
|
763
|
-
return
|
|
783
|
+
return {
|
|
784
|
+
...trackLogsDefaultConfig,
|
|
785
|
+
...this.config?.trackLogs
|
|
786
|
+
};
|
|
787
|
+
}
|
|
788
|
+
getReconnectConfig(type) {
|
|
789
|
+
const config = this.config?.reconnect?.[type] ?? (type === "sip" ? SIP_RECONNECT_CONFIG : SOCKET_RECONNECT_CONFIG);
|
|
790
|
+
return {
|
|
791
|
+
...config
|
|
792
|
+
};
|
|
764
793
|
}
|
|
765
794
|
enableTrackLogs(enabled) {
|
|
766
795
|
this.config.trackLogs.enabled = enabled;
|
|
@@ -929,10 +958,6 @@ var formatGetInviteData = (inviteData) => {
|
|
|
929
958
|
};
|
|
930
959
|
|
|
931
960
|
// core/connect.ts
|
|
932
|
-
var DEFAULT_RECONNECT_CONFIG = {
|
|
933
|
-
maxAttempts: 3,
|
|
934
|
-
delay: 500
|
|
935
|
-
};
|
|
936
961
|
var MAX_HEARTBEAT_COUNT = 6;
|
|
937
962
|
function convertObjectStringToJSON(input) {
|
|
938
963
|
const corrected = input.replace(/(\w+):\s*'(.*?)'/g, '"$1": "$2"').replace(/'/g, '"');
|
|
@@ -964,10 +989,6 @@ var initUserMedia = () => {
|
|
|
964
989
|
};
|
|
965
990
|
var Connect = class {
|
|
966
991
|
callKit;
|
|
967
|
-
/**
|
|
968
|
-
*@description Reconnect config
|
|
969
|
-
*/
|
|
970
|
-
reconnectConfig;
|
|
971
992
|
/**
|
|
972
993
|
*@description Whether muted
|
|
973
994
|
*/
|
|
@@ -1007,11 +1028,9 @@ var Connect = class {
|
|
|
1007
1028
|
hasInvite = false;
|
|
1008
1029
|
constructor(callKit) {
|
|
1009
1030
|
this.callKit = callKit;
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
...reconnect
|
|
1014
|
-
};
|
|
1031
|
+
}
|
|
1032
|
+
get reconnectConfig() {
|
|
1033
|
+
return this.callKit.config.getReconnectConfig("sip");
|
|
1015
1034
|
}
|
|
1016
1035
|
// current call id for invite data
|
|
1017
1036
|
currentCallId = null;
|
|
@@ -1506,9 +1525,7 @@ var Connect = class {
|
|
|
1506
1525
|
version: `${this.callKit.config.getConfig().version}`
|
|
1507
1526
|
}
|
|
1508
1527
|
});
|
|
1509
|
-
await this.registerer.register().
|
|
1510
|
-
this.callKit.socket.send(SocketSendEvent.START);
|
|
1511
|
-
}).catch(async (err) => {
|
|
1528
|
+
await this.registerer.register().catch(async (err) => {
|
|
1512
1529
|
this.callKit.reset();
|
|
1513
1530
|
this.callKit.logger.error(err?.message, {
|
|
1514
1531
|
caller: "Connect.register",
|
|
@@ -1520,7 +1537,6 @@ var Connect = class {
|
|
|
1520
1537
|
});
|
|
1521
1538
|
},
|
|
1522
1539
|
onDisconnect: (error) => {
|
|
1523
|
-
console.log("onDisconnect", error);
|
|
1524
1540
|
if (error) {
|
|
1525
1541
|
this.callKit.logger.info("SIP User Agent Disconnected with error", {
|
|
1526
1542
|
caller: "Connect.register",
|
|
@@ -1654,6 +1670,7 @@ var Connect = class {
|
|
|
1654
1670
|
}
|
|
1655
1671
|
});
|
|
1656
1672
|
});
|
|
1673
|
+
this.setRegister(false);
|
|
1657
1674
|
}
|
|
1658
1675
|
async call(callback) {
|
|
1659
1676
|
this.callKit.logger.info("connect call", {
|
|
@@ -1677,6 +1694,8 @@ var Connect = class {
|
|
|
1677
1694
|
* @param register
|
|
1678
1695
|
*/
|
|
1679
1696
|
setRegister(register) {
|
|
1697
|
+
if (this.isRegister === register)
|
|
1698
|
+
return;
|
|
1680
1699
|
this.callKit.logger.info("connect setRegister", {
|
|
1681
1700
|
caller: "Connect.setRegister",
|
|
1682
1701
|
type: "SIP",
|
|
@@ -1889,33 +1908,46 @@ var Connect = class {
|
|
|
1889
1908
|
};
|
|
1890
1909
|
|
|
1891
1910
|
// core/socket.ts
|
|
1892
|
-
var RECONNECT_CONFIG = {
|
|
1893
|
-
enabled: true,
|
|
1894
|
-
maxAttempts: 3,
|
|
1895
|
-
delay: 1e3,
|
|
1896
|
-
pingInterval: 3e4,
|
|
1897
|
-
pingTimeout: 5e3
|
|
1898
|
-
};
|
|
1899
1911
|
var Socket = class {
|
|
1900
1912
|
callKit;
|
|
1901
1913
|
ws;
|
|
1902
|
-
socketConfig;
|
|
1903
1914
|
lastPingTime = void 0;
|
|
1904
|
-
isConnected = false;
|
|
1905
1915
|
pingTimer;
|
|
1906
|
-
|
|
1907
|
-
|
|
1916
|
+
/**
|
|
1917
|
+
* @description reconnect timer
|
|
1918
|
+
*/
|
|
1908
1919
|
reconnectTimer;
|
|
1909
|
-
|
|
1920
|
+
/**
|
|
1921
|
+
* @description reconnect attempts
|
|
1922
|
+
*/
|
|
1910
1923
|
reconnectAttempts = 0;
|
|
1911
|
-
|
|
1924
|
+
/**
|
|
1925
|
+
* @description connect auth state
|
|
1926
|
+
* @default {
|
|
1927
|
+
* startConfirm: false,
|
|
1928
|
+
* isConnected: false,
|
|
1929
|
+
* isReconnecting: false,
|
|
1930
|
+
* isAuthenticated: false,
|
|
1931
|
+
* isError: false
|
|
1932
|
+
* }
|
|
1933
|
+
*/
|
|
1934
|
+
connectAuthState = {
|
|
1935
|
+
startConfirm: false,
|
|
1936
|
+
isConnected: false,
|
|
1937
|
+
isReconnecting: false,
|
|
1938
|
+
isError: false
|
|
1939
|
+
};
|
|
1940
|
+
get startConfirm() {
|
|
1941
|
+
return this.connectAuthState.startConfirm;
|
|
1942
|
+
}
|
|
1943
|
+
get isError() {
|
|
1944
|
+
return this.connectAuthState.isError;
|
|
1945
|
+
}
|
|
1912
1946
|
constructor(callKit) {
|
|
1913
1947
|
this.callKit = callKit;
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
...reconnect
|
|
1918
|
-
};
|
|
1948
|
+
}
|
|
1949
|
+
get reconnectConfig() {
|
|
1950
|
+
return this.callKit.config.getReconnectConfig("incall");
|
|
1919
1951
|
}
|
|
1920
1952
|
init() {
|
|
1921
1953
|
const { socket } = this.callKit.config.getConfig();
|
|
@@ -1928,19 +1960,25 @@ var Socket = class {
|
|
|
1928
1960
|
});
|
|
1929
1961
|
this.connect(socket);
|
|
1930
1962
|
}
|
|
1963
|
+
setConnectAuthState(key, value) {
|
|
1964
|
+
if (this.connectAuthState[key] === value)
|
|
1965
|
+
return;
|
|
1966
|
+
this.connectAuthState[key] = value;
|
|
1967
|
+
}
|
|
1931
1968
|
handleDisconnect() {
|
|
1932
|
-
this.isConnected
|
|
1933
|
-
|
|
1934
|
-
|
|
1969
|
+
this.setConnectAuthState("isConnected", false);
|
|
1970
|
+
const { enabled } = this.reconnectConfig;
|
|
1971
|
+
if (!this.callKit.config.isLogin() || !enabled) {
|
|
1972
|
+
this.callKit.reset();
|
|
1935
1973
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
1936
1974
|
event: "INCALL_NOT_CONNECTED"
|
|
1937
1975
|
});
|
|
1938
1976
|
return;
|
|
1939
1977
|
}
|
|
1940
|
-
if (this.
|
|
1978
|
+
if (this.connectAuthState.isReconnecting) {
|
|
1941
1979
|
return;
|
|
1942
1980
|
}
|
|
1943
|
-
if (this.
|
|
1981
|
+
if (this.connectAuthState.isError) {
|
|
1944
1982
|
return;
|
|
1945
1983
|
}
|
|
1946
1984
|
this.attemptReconnect();
|
|
@@ -1961,7 +1999,7 @@ var Socket = class {
|
|
|
1961
1999
|
});
|
|
1962
2000
|
}
|
|
1963
2001
|
this.ws = void 0;
|
|
1964
|
-
this.isConnected
|
|
2002
|
+
this.setConnectAuthState("isConnected", false);
|
|
1965
2003
|
}
|
|
1966
2004
|
connect(socketUrl) {
|
|
1967
2005
|
if (this.ws) {
|
|
@@ -1979,11 +2017,12 @@ var Socket = class {
|
|
|
1979
2017
|
type: "INCALL",
|
|
1980
2018
|
content: { ev }
|
|
1981
2019
|
});
|
|
1982
|
-
this.
|
|
1983
|
-
this.isConnected = true;
|
|
2020
|
+
this.setConnectAuthState("isConnected", true);
|
|
1984
2021
|
this.lastPingTime = Date.now();
|
|
1985
2022
|
this.checkPing();
|
|
1986
|
-
|
|
2023
|
+
this.send(SocketSendEvent.START);
|
|
2024
|
+
if (this.connectAuthState.isReconnecting) {
|
|
2025
|
+
this.setConnectAuthState("isReconnecting", false);
|
|
1987
2026
|
this.callKit.logger.info("reconnect success", {
|
|
1988
2027
|
caller: "Socket.onOpen",
|
|
1989
2028
|
type: "INCALL",
|
|
@@ -1996,15 +2035,32 @@ var Socket = class {
|
|
|
1996
2035
|
event: "INCALL_RECONNECT_SUCCESS"
|
|
1997
2036
|
});
|
|
1998
2037
|
}
|
|
1999
|
-
this.resetReconnectState();
|
|
2000
2038
|
}
|
|
2001
|
-
|
|
2002
|
-
this.isReconnecting = false;
|
|
2039
|
+
cleanReconnectState() {
|
|
2003
2040
|
this.reconnectAttempts = 0;
|
|
2004
2041
|
if (this.reconnectTimer) {
|
|
2005
2042
|
clearTimeout(this.reconnectTimer);
|
|
2006
2043
|
this.reconnectTimer = void 0;
|
|
2007
2044
|
}
|
|
2045
|
+
this.setConnectAuthState("isReconnecting", false);
|
|
2046
|
+
this.setConnectAuthState("isError", false);
|
|
2047
|
+
}
|
|
2048
|
+
resetConnectState() {
|
|
2049
|
+
this.connectAuthState = {
|
|
2050
|
+
startConfirm: false,
|
|
2051
|
+
isConnected: false,
|
|
2052
|
+
isReconnecting: false,
|
|
2053
|
+
isError: false
|
|
2054
|
+
};
|
|
2055
|
+
this.cleanReconnectState();
|
|
2056
|
+
this.callKit.logger.info("reset connect state", {
|
|
2057
|
+
caller: "Socket.resetConnectState",
|
|
2058
|
+
type: "INCALL",
|
|
2059
|
+
content: {
|
|
2060
|
+
reconnectAttempts: this.reconnectAttempts,
|
|
2061
|
+
connectAuthState: this.connectAuthState
|
|
2062
|
+
}
|
|
2063
|
+
});
|
|
2008
2064
|
}
|
|
2009
2065
|
onClose(ev) {
|
|
2010
2066
|
this.callKit.logger.info("socket onClose", {
|
|
@@ -2015,7 +2071,6 @@ var Socket = class {
|
|
|
2015
2071
|
this.handleDisconnect();
|
|
2016
2072
|
}
|
|
2017
2073
|
onError(ev) {
|
|
2018
|
-
this.socketError = true;
|
|
2019
2074
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2020
2075
|
event: "INCALL_CONNECT_ERROR",
|
|
2021
2076
|
err: ev
|
|
@@ -2054,7 +2109,7 @@ var Socket = class {
|
|
|
2054
2109
|
});
|
|
2055
2110
|
content = data.data;
|
|
2056
2111
|
}
|
|
2057
|
-
this.callKit.logger.info(
|
|
2112
|
+
this.callKit.logger.info(`socket onMessage: ${data.event}`, {
|
|
2058
2113
|
caller: "Socket.onMessage",
|
|
2059
2114
|
type: "INCALL",
|
|
2060
2115
|
content: {
|
|
@@ -2068,134 +2123,39 @@ var Socket = class {
|
|
|
2068
2123
|
return;
|
|
2069
2124
|
}
|
|
2070
2125
|
if (data.event === SocketReceiveEvent.START_CONFIRM) {
|
|
2071
|
-
this.
|
|
2072
|
-
|
|
2073
|
-
type: "INCALL",
|
|
2074
|
-
content: {
|
|
2075
|
-
data: content,
|
|
2076
|
-
event: SocketReceiveEvent.START_CONFIRM
|
|
2077
|
-
}
|
|
2078
|
-
});
|
|
2079
|
-
this.satrtConfirm = true;
|
|
2080
|
-
}
|
|
2081
|
-
if (data.event === SocketReceiveEvent.CALL_SUCCESS) {
|
|
2082
|
-
this.callKit.logger.info("call success", {
|
|
2083
|
-
caller: "Socket.onMessage",
|
|
2084
|
-
type: "INCALL",
|
|
2085
|
-
content: {
|
|
2086
|
-
data: content,
|
|
2087
|
-
event: SocketReceiveEvent.CALL_SUCCESS
|
|
2088
|
-
}
|
|
2089
|
-
});
|
|
2090
|
-
}
|
|
2091
|
-
if (data.event === SocketReceiveEvent.CALL_FAILED) {
|
|
2092
|
-
this.callKit.logger.info(data.msg, {
|
|
2093
|
-
caller: "Socket.onMessage",
|
|
2094
|
-
type: "INCALL",
|
|
2095
|
-
content: {
|
|
2096
|
-
data: content,
|
|
2097
|
-
errCode: ErrorCode.SOCKET_CALL_ERROR
|
|
2098
|
-
}
|
|
2099
|
-
});
|
|
2126
|
+
this.setConnectAuthState("startConfirm", true);
|
|
2127
|
+
this.cleanReconnectState();
|
|
2100
2128
|
}
|
|
2101
2129
|
if (data.event === SocketReceiveEvent.CUSTOMER_RINGING) {
|
|
2102
2130
|
this.callKit.trigger(KitEvent.CALL_RINGING, /* @__PURE__ */ new Date());
|
|
2103
|
-
this.callKit.logger.info(data.msg, {
|
|
2104
|
-
caller: `Socket.onMessage:${data.event}`,
|
|
2105
|
-
type: "INCALL",
|
|
2106
|
-
content: {
|
|
2107
|
-
data: content,
|
|
2108
|
-
event: SocketReceiveEvent.CUSTOMER_RINGING
|
|
2109
|
-
}
|
|
2110
|
-
});
|
|
2111
2131
|
}
|
|
2112
2132
|
if (data.event === SocketReceiveEvent.CUSTOMER_PICK_UP) {
|
|
2113
|
-
this.callKit.logger.info(data.msg, {
|
|
2114
|
-
caller: "Socket.onMessage",
|
|
2115
|
-
type: "INCALL",
|
|
2116
|
-
content: {
|
|
2117
|
-
data: content,
|
|
2118
|
-
event: SocketReceiveEvent.CUSTOMER_PICK_UP
|
|
2119
|
-
}
|
|
2120
|
-
});
|
|
2121
2133
|
this.callKit.trigger(KitEvent.CALL_PICK_UP, /* @__PURE__ */ new Date());
|
|
2122
2134
|
}
|
|
2123
2135
|
if (data.event === SocketReceiveEvent.AGENT_PICK_UP) {
|
|
2124
|
-
this.callKit.logger.info(data.msg, {
|
|
2125
|
-
caller: "Socket.onMessage",
|
|
2126
|
-
type: "INCALL",
|
|
2127
|
-
content: {
|
|
2128
|
-
data: content,
|
|
2129
|
-
event: SocketReceiveEvent.AGENT_PICK_UP
|
|
2130
|
-
}
|
|
2131
|
-
});
|
|
2132
2136
|
this.callKit.trigger(KitEvent.AGENT_PICK_UP, /* @__PURE__ */ new Date());
|
|
2133
2137
|
}
|
|
2134
2138
|
if (data.event === SocketReceiveEvent.CUSTOMER_HANG_UP) {
|
|
2135
|
-
this.callKit.logger.info(data.msg, {
|
|
2136
|
-
caller: `Socket.onMessage:${data.event}`,
|
|
2137
|
-
type: "INCALL",
|
|
2138
|
-
content: {
|
|
2139
|
-
data: content,
|
|
2140
|
-
event: SocketReceiveEvent.CUSTOMER_HANG_UP
|
|
2141
|
-
}
|
|
2142
|
-
});
|
|
2143
2139
|
this.callKit.trigger(KitEvent.CALL_HANG_UP, /* @__PURE__ */ new Date());
|
|
2144
2140
|
if (content?.callUuid) {
|
|
2145
2141
|
this.callKit.connect.socketTriggerHangup(content.callUuid);
|
|
2146
2142
|
}
|
|
2147
2143
|
}
|
|
2148
2144
|
if (data.event === SocketReceiveEvent.CUSTOMER_NO_ANSWER) {
|
|
2149
|
-
this.callKit.logger.info(data.msg, {
|
|
2150
|
-
caller: "Socket.onMessage",
|
|
2151
|
-
type: "INCALL",
|
|
2152
|
-
content: {
|
|
2153
|
-
data: content,
|
|
2154
|
-
event: SocketReceiveEvent.CUSTOMER_NO_ANSWER
|
|
2155
|
-
}
|
|
2156
|
-
});
|
|
2157
2145
|
this.callKit.trigger(KitEvent.CALL_NO_ANSWER);
|
|
2158
2146
|
if (content?.callUuid) {
|
|
2159
2147
|
this.callKit.connect.socketTriggerHangup(content.callUuid);
|
|
2160
2148
|
}
|
|
2161
2149
|
}
|
|
2162
2150
|
if (data.event === SocketReceiveEvent.CALL_CDR) {
|
|
2163
|
-
this.callKit.logger.info(data.msg, {
|
|
2164
|
-
caller: `Socket.onMessage:${data.event}`,
|
|
2165
|
-
type: "INCALL",
|
|
2166
|
-
content: {
|
|
2167
|
-
data: content,
|
|
2168
|
-
event: SocketReceiveEvent.CALL_CDR
|
|
2169
|
-
}
|
|
2170
|
-
});
|
|
2171
2151
|
this.callKit.trigger(KitEvent.CALL_CDR, content);
|
|
2172
2152
|
}
|
|
2173
|
-
if (data.event === SocketReceiveEvent.STOP_CONFIRM) {
|
|
2174
|
-
this.callKit.logger.info(data.msg, {
|
|
2175
|
-
caller: `Socket.onMessage:${data.event}`,
|
|
2176
|
-
type: "INCALL",
|
|
2177
|
-
content: {
|
|
2178
|
-
data: content,
|
|
2179
|
-
event: SocketReceiveEvent.STOP_CONFIRM
|
|
2180
|
-
}
|
|
2181
|
-
});
|
|
2182
|
-
}
|
|
2183
2153
|
if (data.event === SocketReceiveEvent.CLOSE) {
|
|
2184
2154
|
const { userInfo } = this.callKit.config.getConfig();
|
|
2185
|
-
this.
|
|
2186
|
-
caller: `Socket.onMessage:${data.event}`,
|
|
2187
|
-
type: "INCALL",
|
|
2188
|
-
content: {
|
|
2189
|
-
data: content,
|
|
2190
|
-
event: SocketReceiveEvent.CLOSE
|
|
2191
|
-
}
|
|
2192
|
-
});
|
|
2193
|
-
this.send(SocketSendEvent.END, {
|
|
2194
|
-
agentId: userInfo.agentId
|
|
2195
|
-
});
|
|
2155
|
+
this.send(SocketSendEvent.END, { agentId: userInfo.agentId });
|
|
2196
2156
|
}
|
|
2197
2157
|
if (data.event === SocketReceiveEvent.ERROR) {
|
|
2198
|
-
this.
|
|
2158
|
+
this.setConnectAuthState("isError", true);
|
|
2199
2159
|
this.callKit.reset();
|
|
2200
2160
|
this.callKit.logger.error(data.msg, {
|
|
2201
2161
|
caller: `Socket.onMessage:${data.event}`,
|
|
@@ -2207,7 +2167,7 @@ var Socket = class {
|
|
|
2207
2167
|
});
|
|
2208
2168
|
}
|
|
2209
2169
|
if (data.event === SocketReceiveEvent.SESSION_ERROR) {
|
|
2210
|
-
this.
|
|
2170
|
+
this.setConnectAuthState("isError", true);
|
|
2211
2171
|
this.callKit.reset();
|
|
2212
2172
|
this.callKit.logger.error(data.msg, {
|
|
2213
2173
|
caller: `Socket.onMessage:${data.event}`,
|
|
@@ -2219,14 +2179,6 @@ var Socket = class {
|
|
|
2219
2179
|
});
|
|
2220
2180
|
}
|
|
2221
2181
|
if (data.event === SocketReceiveEvent.AGENT_NO_ANSWER) {
|
|
2222
|
-
this.callKit.logger.info(data.msg, {
|
|
2223
|
-
caller: `Socket.onMessage:${data.event}`,
|
|
2224
|
-
type: "INCALL",
|
|
2225
|
-
content: {
|
|
2226
|
-
data: content,
|
|
2227
|
-
event: SocketReceiveEvent.AGENT_NO_ANSWER
|
|
2228
|
-
}
|
|
2229
|
-
});
|
|
2230
2182
|
if (content?.callUuid) {
|
|
2231
2183
|
this.callKit.connect.socketTriggerHangup(content.callUuid);
|
|
2232
2184
|
}
|
|
@@ -2234,7 +2186,7 @@ var Socket = class {
|
|
|
2234
2186
|
this.callKit.trigger(KitEvent.SERVER_SOCKET_EVENT, data);
|
|
2235
2187
|
}
|
|
2236
2188
|
send(event, message) {
|
|
2237
|
-
if (!this.isConnected) {
|
|
2189
|
+
if (!this.connectAuthState.isConnected) {
|
|
2238
2190
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2239
2191
|
event: "INCALL_NOT_CONNECTED"
|
|
2240
2192
|
});
|
|
@@ -2293,23 +2245,16 @@ var Socket = class {
|
|
|
2293
2245
|
}
|
|
2294
2246
|
}
|
|
2295
2247
|
ping() {
|
|
2296
|
-
if (!this.isConnected)
|
|
2248
|
+
if (!this.connectAuthState.isConnected)
|
|
2297
2249
|
return;
|
|
2298
2250
|
this.send(SocketSendEvent.PING);
|
|
2299
|
-
this.callKit.logger.info(`socket ping`, {
|
|
2300
|
-
caller: "Socket.ping",
|
|
2301
|
-
type: "INCALL",
|
|
2302
|
-
content: {
|
|
2303
|
-
lastPingTime: this.lastPingTime
|
|
2304
|
-
}
|
|
2305
|
-
});
|
|
2306
2251
|
const now = Date.now();
|
|
2307
|
-
const { pingInterval, pingTimeout } = this.
|
|
2252
|
+
const { pingInterval, pingTimeout } = this.reconnectConfig;
|
|
2308
2253
|
if (now - this.lastPingTime > pingInterval + pingTimeout) {
|
|
2309
|
-
if (this.ws && this.isConnected) {
|
|
2254
|
+
if (this.ws && this.connectAuthState.isConnected) {
|
|
2310
2255
|
this.ws.close(4001, "ping timeout");
|
|
2311
2256
|
} else {
|
|
2312
|
-
this.reset();
|
|
2257
|
+
this.callKit.reset();
|
|
2313
2258
|
}
|
|
2314
2259
|
this.callKit.logger.error("socket ping timeout", {
|
|
2315
2260
|
caller: "Socket.ping",
|
|
@@ -2325,21 +2270,28 @@ var Socket = class {
|
|
|
2325
2270
|
clearInterval(this.pingTimer);
|
|
2326
2271
|
}
|
|
2327
2272
|
this.ping();
|
|
2273
|
+
const { pingInterval } = this.reconnectConfig;
|
|
2328
2274
|
this.pingTimer = setInterval(() => {
|
|
2329
2275
|
this.ping();
|
|
2330
|
-
},
|
|
2276
|
+
}, pingInterval);
|
|
2331
2277
|
}
|
|
2332
2278
|
/**
|
|
2333
2279
|
* reset socket connection and all states
|
|
2334
2280
|
*/
|
|
2335
|
-
async reset() {
|
|
2281
|
+
async reset(config) {
|
|
2282
|
+
const { force = false } = config || {};
|
|
2336
2283
|
if (this.pingTimer) {
|
|
2337
2284
|
clearInterval(this.pingTimer);
|
|
2338
2285
|
this.pingTimer = void 0;
|
|
2339
2286
|
}
|
|
2340
|
-
|
|
2287
|
+
if (force) {
|
|
2288
|
+
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2289
|
+
event: "INCALL_RESET"
|
|
2290
|
+
});
|
|
2291
|
+
this.resetConnectState();
|
|
2292
|
+
}
|
|
2341
2293
|
this.lastPingTime = void 0;
|
|
2342
|
-
this.
|
|
2294
|
+
this.setConnectAuthState("startConfirm", false);
|
|
2343
2295
|
this.clearWebSocket();
|
|
2344
2296
|
}
|
|
2345
2297
|
attemptReconnect() {
|
|
@@ -2347,11 +2299,12 @@ var Socket = class {
|
|
|
2347
2299
|
clearTimeout(this.reconnectTimer);
|
|
2348
2300
|
this.reconnectTimer = void 0;
|
|
2349
2301
|
}
|
|
2350
|
-
|
|
2302
|
+
const { maxAttempts } = this.reconnectConfig;
|
|
2303
|
+
if (this.reconnectAttempts >= maxAttempts) {
|
|
2351
2304
|
this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
|
|
2352
2305
|
event: "INCALL_RECONNECT_ERROR"
|
|
2353
2306
|
});
|
|
2354
|
-
this.reset();
|
|
2307
|
+
this.callKit.reset();
|
|
2355
2308
|
this.callKit.logger.error("Maximum reconnection attempts reached", {
|
|
2356
2309
|
caller: "Socket.attemptReconnect",
|
|
2357
2310
|
type: "INCALL",
|
|
@@ -2367,17 +2320,17 @@ var Socket = class {
|
|
|
2367
2320
|
event: "INCALL_RECONNECT_START"
|
|
2368
2321
|
});
|
|
2369
2322
|
}
|
|
2370
|
-
this.isReconnecting
|
|
2323
|
+
this.setConnectAuthState("isReconnecting", true);
|
|
2371
2324
|
this.reconnectAttempts += 1;
|
|
2372
|
-
const { delay } = this.
|
|
2325
|
+
const { delay } = this.reconnectConfig;
|
|
2373
2326
|
this.callKit.logger.info(
|
|
2374
|
-
`Preparing reconnection attempt ${this.reconnectAttempts}/${
|
|
2327
|
+
`Preparing reconnection attempt ${this.reconnectAttempts}/${maxAttempts}, delay: ${delay}ms`,
|
|
2375
2328
|
{
|
|
2376
2329
|
caller: "Socket.attemptReconnect",
|
|
2377
2330
|
type: "INCALL",
|
|
2378
2331
|
content: {
|
|
2379
2332
|
reconnectAttempts: this.reconnectAttempts,
|
|
2380
|
-
maxAttempts
|
|
2333
|
+
maxAttempts,
|
|
2381
2334
|
delay
|
|
2382
2335
|
}
|
|
2383
2336
|
}
|
|
@@ -2404,17 +2357,34 @@ var CallKit = class {
|
|
|
2404
2357
|
this.connect = new Connect(this);
|
|
2405
2358
|
this.callCenter = new Call(this);
|
|
2406
2359
|
this.socket = new Socket(this);
|
|
2407
|
-
this.
|
|
2408
|
-
|
|
2409
|
-
|
|
2410
|
-
|
|
2360
|
+
this.logger = new Logger(this, options.log);
|
|
2361
|
+
if (options.log) {
|
|
2362
|
+
this.config.setConfig("log", options.log);
|
|
2363
|
+
}
|
|
2364
|
+
if (options.trackLogs) {
|
|
2365
|
+
this.config.setConfig("trackLogs", {
|
|
2366
|
+
...trackLogsDefaultConfig,
|
|
2367
|
+
...options.trackLogs
|
|
2368
|
+
});
|
|
2369
|
+
}
|
|
2370
|
+
if (options.audioRef) {
|
|
2371
|
+
this.config.setConfig("audioRef", options.audioRef);
|
|
2372
|
+
}
|
|
2373
|
+
if (options.host) {
|
|
2374
|
+
this.config.setConfig("host", options.host);
|
|
2375
|
+
}
|
|
2411
2376
|
this.config.setConfig(
|
|
2412
2377
|
"constrains",
|
|
2413
2378
|
options.constrains || constrainsDefault
|
|
2414
2379
|
);
|
|
2415
2380
|
this.config.setConfig("socket", options.socket);
|
|
2416
|
-
this.config.setConfig("reconnect",
|
|
2417
|
-
|
|
2381
|
+
this.config.setConfig("reconnect", {
|
|
2382
|
+
sip: { ...SIP_RECONNECT_CONFIG, ...options.reconnect?.sip || {} },
|
|
2383
|
+
incall: {
|
|
2384
|
+
...SOCKET_RECONNECT_CONFIG,
|
|
2385
|
+
...options.reconnect?.incall || {}
|
|
2386
|
+
}
|
|
2387
|
+
});
|
|
2418
2388
|
this.logger.info("callKit init", {
|
|
2419
2389
|
caller: "CallKit.init",
|
|
2420
2390
|
content: options
|
|
@@ -2452,6 +2422,13 @@ var CallKit = class {
|
|
|
2452
2422
|
encryptionPassword
|
|
2453
2423
|
}
|
|
2454
2424
|
});
|
|
2425
|
+
if (this.socket.isError) {
|
|
2426
|
+
this.logger.warn("socket is error", {
|
|
2427
|
+
caller: "CallKit.login",
|
|
2428
|
+
content: { username, password, extra, socketError: this.socket.isError }
|
|
2429
|
+
});
|
|
2430
|
+
return;
|
|
2431
|
+
}
|
|
2455
2432
|
try {
|
|
2456
2433
|
const user = await this.api.login({
|
|
2457
2434
|
userName: username,
|
|
@@ -2503,7 +2480,7 @@ var CallKit = class {
|
|
|
2503
2480
|
try {
|
|
2504
2481
|
await this.api.loginOut({ sessionId });
|
|
2505
2482
|
} catch (error) {
|
|
2506
|
-
this.logger.
|
|
2483
|
+
this.logger.warn(error, {
|
|
2507
2484
|
caller: "CallKit.logout",
|
|
2508
2485
|
content: {
|
|
2509
2486
|
errCode: ErrorCode.API_USER_LOGOUT_ERROR
|
|
@@ -2513,8 +2490,9 @@ var CallKit = class {
|
|
|
2513
2490
|
}
|
|
2514
2491
|
if (isReset) {
|
|
2515
2492
|
await this.reset();
|
|
2493
|
+
} else {
|
|
2494
|
+
this.config.reset();
|
|
2516
2495
|
}
|
|
2517
|
-
this.trigger(KitEvent.KIT_LOGIN_CHANGE, false);
|
|
2518
2496
|
}
|
|
2519
2497
|
async call(extno = "", options = {
|
|
2520
2498
|
sourceType: CallSourceType.phoneNum,
|
|
@@ -2579,9 +2557,6 @@ var CallKit = class {
|
|
|
2579
2557
|
});
|
|
2580
2558
|
this.connect.unregister();
|
|
2581
2559
|
}
|
|
2582
|
-
async stop() {
|
|
2583
|
-
await this.connect.stop();
|
|
2584
|
-
}
|
|
2585
2560
|
async hangup() {
|
|
2586
2561
|
if (!this.config.check())
|
|
2587
2562
|
return;
|
|
@@ -2664,11 +2639,19 @@ var CallKit = class {
|
|
|
2664
2639
|
userStatus: status
|
|
2665
2640
|
});
|
|
2666
2641
|
}
|
|
2667
|
-
|
|
2642
|
+
/**
|
|
2643
|
+
* reset callkit
|
|
2644
|
+
* @description recover the callkit to the initial state
|
|
2645
|
+
* @default force is false
|
|
2646
|
+
* @param config.force is true, the callkit reset socket connection and all states
|
|
2647
|
+
*/
|
|
2648
|
+
async reset(config) {
|
|
2649
|
+
const { force = false } = config || {};
|
|
2668
2650
|
this.logger.info("reset", {
|
|
2669
2651
|
caller: "CallKit.reset",
|
|
2670
2652
|
content: {
|
|
2671
|
-
connectStatus: this.connect.connectStatus
|
|
2653
|
+
connectStatus: this.connect.connectStatus,
|
|
2654
|
+
force
|
|
2672
2655
|
}
|
|
2673
2656
|
});
|
|
2674
2657
|
if (this.connect.isCalling()) {
|
|
@@ -2677,9 +2660,10 @@ var CallKit = class {
|
|
|
2677
2660
|
await this.connect.reset();
|
|
2678
2661
|
if (this.config.isLogin()) {
|
|
2679
2662
|
await this.logout({ isReset: false });
|
|
2663
|
+
} else {
|
|
2664
|
+
await this.config.reset();
|
|
2680
2665
|
}
|
|
2681
|
-
await this.
|
|
2682
|
-
await this.socket.reset();
|
|
2666
|
+
await this.socket.reset({ force });
|
|
2683
2667
|
}
|
|
2684
2668
|
on(event, callback) {
|
|
2685
2669
|
this.listener.push({
|