@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.js CHANGED
@@ -645,13 +645,13 @@ var Call = class {
645
645
  // package.json
646
646
  var package_default = {
647
647
  name: "@koi-design/callkit",
648
- version: "2.0.4",
648
+ version: "2.0.5",
649
649
  description: "callkit",
650
650
  author: "koi",
651
651
  license: "ISC",
652
652
  scripts: {
653
653
  build: "tsup",
654
- "build:watch": "tsup --watch",
654
+ "build:w": "tsup --watch",
655
655
  dev: "vite",
656
656
  lint: "eslint -c ../../.eslintrc.js --ext .jsx,.js,.tsx,.ts ./package --fix",
657
657
  release: "tsup && node scripts/pkg.js"
@@ -1926,23 +1926,41 @@ var RECONNECT_CONFIG = {
1926
1926
  var Socket = class {
1927
1927
  callKit;
1928
1928
  ws;
1929
- socketConfig;
1930
1929
  lastPingTime = void 0;
1931
- isConnected = false;
1932
1930
  pingTimer;
1933
- // Whether received start confirmation
1934
- satrtConfirm = false;
1931
+ /**
1932
+ * @description reconnect timer
1933
+ */
1935
1934
  reconnectTimer;
1936
- isReconnecting = false;
1935
+ /**
1936
+ * @description reconnect attempts
1937
+ */
1937
1938
  reconnectAttempts = 0;
1938
- socketError = false;
1939
+ /**
1940
+ * @description connect auth state
1941
+ * @default {
1942
+ * satrtConfirm: false,
1943
+ * isConnected: false,
1944
+ * isReconnecting: false,
1945
+ * isAuthenticated: false,
1946
+ * isError: false
1947
+ * }
1948
+ */
1949
+ connectAuthState = {
1950
+ satrtConfirm: false,
1951
+ isConnected: false,
1952
+ isReconnecting: false,
1953
+ isAuthenticated: false,
1954
+ isError: false
1955
+ };
1956
+ get satrtConfirm() {
1957
+ return this.connectAuthState.satrtConfirm;
1958
+ }
1959
+ get isError() {
1960
+ return this.connectAuthState.isError;
1961
+ }
1939
1962
  constructor(callKit) {
1940
1963
  this.callKit = callKit;
1941
- const { reconnect } = this.callKit.config.getConfig();
1942
- this.socketConfig = {
1943
- ...RECONNECT_CONFIG,
1944
- ...reconnect
1945
- };
1946
1964
  }
1947
1965
  init() {
1948
1966
  const { socket } = this.callKit.config.getConfig();
@@ -1955,19 +1973,29 @@ var Socket = class {
1955
1973
  });
1956
1974
  this.connect(socket);
1957
1975
  }
1976
+ getSocketConfig() {
1977
+ const { reconnect } = this.callKit.config.getConfig();
1978
+ return {
1979
+ ...RECONNECT_CONFIG,
1980
+ ...reconnect
1981
+ };
1982
+ }
1983
+ setConnectAuthState(key, value) {
1984
+ if (this.connectAuthState[key] === value)
1985
+ return;
1986
+ this.connectAuthState[key] = value;
1987
+ }
1958
1988
  handleDisconnect() {
1959
- this.isConnected = false;
1960
- if (!this.callKit.config.isLogin() || !this.socketConfig.enabled) {
1961
- this.reset();
1989
+ this.setConnectAuthState("isConnected", false);
1990
+ const { enabled } = this.getSocketConfig();
1991
+ if (!this.callKit.config.isLogin() || !enabled) {
1992
+ this.callKit.reset();
1962
1993
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
1963
1994
  event: "INCALL_NOT_CONNECTED"
1964
1995
  });
1965
1996
  return;
1966
1997
  }
1967
- if (this.socketError) {
1968
- return;
1969
- }
1970
- if (this.isReconnecting) {
1998
+ if (this.connectAuthState.isReconnecting) {
1971
1999
  return;
1972
2000
  }
1973
2001
  this.attemptReconnect();
@@ -1988,7 +2016,7 @@ var Socket = class {
1988
2016
  });
1989
2017
  }
1990
2018
  this.ws = void 0;
1991
- this.isConnected = false;
2019
+ this.setConnectAuthState("isConnected", false);
1992
2020
  }
1993
2021
  connect(socketUrl) {
1994
2022
  if (this.ws) {
@@ -2006,11 +2034,11 @@ var Socket = class {
2006
2034
  type: "INCALL",
2007
2035
  content: { ev }
2008
2036
  });
2009
- this.socketError = false;
2010
- this.isConnected = true;
2037
+ this.setConnectAuthState("isConnected", true);
2011
2038
  this.lastPingTime = Date.now();
2012
2039
  this.checkPing();
2013
- if (this.isReconnecting) {
2040
+ if (this.connectAuthState.isReconnecting) {
2041
+ this.setConnectAuthState("isReconnecting", false);
2014
2042
  this.callKit.logger.info("reconnect success", {
2015
2043
  caller: "Socket.onOpen",
2016
2044
  type: "INCALL",
@@ -2023,10 +2051,15 @@ var Socket = class {
2023
2051
  event: "INCALL_RECONNECT_SUCCESS"
2024
2052
  });
2025
2053
  }
2026
- this.resetReconnectState();
2027
2054
  }
2028
- resetReconnectState() {
2029
- this.isReconnecting = false;
2055
+ resetConnectState() {
2056
+ this.connectAuthState = {
2057
+ satrtConfirm: false,
2058
+ isConnected: false,
2059
+ isReconnecting: false,
2060
+ isAuthenticated: false,
2061
+ isError: false
2062
+ };
2030
2063
  this.reconnectAttempts = 0;
2031
2064
  if (this.reconnectTimer) {
2032
2065
  clearTimeout(this.reconnectTimer);
@@ -2042,7 +2075,6 @@ var Socket = class {
2042
2075
  this.handleDisconnect();
2043
2076
  }
2044
2077
  onError(ev) {
2045
- this.socketError = true;
2046
2078
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2047
2079
  event: "INCALL_CONNECT_ERROR",
2048
2080
  err: ev
@@ -2092,6 +2124,20 @@ var Socket = class {
2092
2124
  this.confirmAck(data);
2093
2125
  if (data.event === SocketReceiveEvent.PONG) {
2094
2126
  this.lastPingTime = Date.now();
2127
+ this.setConnectAuthState("isAuthenticated", true);
2128
+ this.callKit.logger.info("socket onMessage pong Authenticated", {
2129
+ caller: "Socket.onMessage",
2130
+ type: "INCALL",
2131
+ content: {
2132
+ data: content,
2133
+ event: SocketReceiveEvent.PONG,
2134
+ isAuthenticated: true
2135
+ }
2136
+ });
2137
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2138
+ event: "INCALL_AUTHENTICATED",
2139
+ isAuthenticated: true
2140
+ });
2095
2141
  return;
2096
2142
  }
2097
2143
  if (data.event === SocketReceiveEvent.START_CONFIRM) {
@@ -2103,7 +2149,7 @@ var Socket = class {
2103
2149
  event: SocketReceiveEvent.START_CONFIRM
2104
2150
  }
2105
2151
  });
2106
- this.satrtConfirm = true;
2152
+ this.setConnectAuthState("satrtConfirm", true);
2107
2153
  }
2108
2154
  if (data.event === SocketReceiveEvent.CALL_SUCCESS) {
2109
2155
  this.callKit.logger.info("call success", {
@@ -2217,12 +2263,10 @@ var Socket = class {
2217
2263
  event: SocketReceiveEvent.CLOSE
2218
2264
  }
2219
2265
  });
2220
- this.send(SocketSendEvent.END, {
2221
- agentId: userInfo.agentId
2222
- });
2266
+ this.send(SocketSendEvent.END, { agentId: userInfo.agentId });
2223
2267
  }
2224
2268
  if (data.event === SocketReceiveEvent.ERROR) {
2225
- this.socketError = true;
2269
+ this.setConnectAuthState("isError", true);
2226
2270
  this.callKit.reset();
2227
2271
  this.callKit.logger.error(data.msg, {
2228
2272
  caller: `Socket.onMessage:${data.event}`,
@@ -2234,7 +2278,7 @@ var Socket = class {
2234
2278
  });
2235
2279
  }
2236
2280
  if (data.event === SocketReceiveEvent.SESSION_ERROR) {
2237
- this.socketError = true;
2281
+ this.setConnectAuthState("isError", true);
2238
2282
  this.callKit.reset();
2239
2283
  this.callKit.logger.error(data.msg, {
2240
2284
  caller: `Socket.onMessage:${data.event}`,
@@ -2261,7 +2305,7 @@ var Socket = class {
2261
2305
  this.callKit.trigger(KitEvent.SERVER_SOCKET_EVENT, data);
2262
2306
  }
2263
2307
  send(event, message) {
2264
- if (!this.isConnected) {
2308
+ if (!this.connectAuthState.isConnected) {
2265
2309
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2266
2310
  event: "INCALL_NOT_CONNECTED"
2267
2311
  });
@@ -2320,7 +2364,7 @@ var Socket = class {
2320
2364
  }
2321
2365
  }
2322
2366
  ping() {
2323
- if (!this.isConnected)
2367
+ if (!this.connectAuthState.isConnected)
2324
2368
  return;
2325
2369
  this.send(SocketSendEvent.PING);
2326
2370
  this.callKit.logger.info(`socket ping`, {
@@ -2331,12 +2375,12 @@ var Socket = class {
2331
2375
  }
2332
2376
  });
2333
2377
  const now = Date.now();
2334
- const { pingInterval, pingTimeout } = this.socketConfig;
2378
+ const { pingInterval, pingTimeout } = this.getSocketConfig();
2335
2379
  if (now - this.lastPingTime > pingInterval + pingTimeout) {
2336
- if (this.ws && this.isConnected) {
2380
+ if (this.ws && this.connectAuthState.isConnected) {
2337
2381
  this.ws.close(4001, "ping timeout");
2338
2382
  } else {
2339
- this.reset();
2383
+ this.callKit.reset();
2340
2384
  }
2341
2385
  this.callKit.logger.error("socket ping timeout", {
2342
2386
  caller: "Socket.ping",
@@ -2352,21 +2396,29 @@ var Socket = class {
2352
2396
  clearInterval(this.pingTimer);
2353
2397
  }
2354
2398
  this.ping();
2399
+ const { pingInterval } = this.getSocketConfig();
2355
2400
  this.pingTimer = setInterval(() => {
2356
2401
  this.ping();
2357
- }, this.socketConfig.pingInterval);
2402
+ }, pingInterval);
2358
2403
  }
2359
2404
  /**
2360
2405
  * reset socket connection and all states
2361
2406
  */
2362
- async reset() {
2407
+ async reset(config) {
2408
+ const { focus = false } = config || {};
2363
2409
  if (this.pingTimer) {
2364
2410
  clearInterval(this.pingTimer);
2365
2411
  this.pingTimer = void 0;
2366
2412
  }
2367
- this.resetReconnectState();
2413
+ if (focus) {
2414
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2415
+ event: "INCALL_RESET"
2416
+ });
2417
+ this.resetConnectState();
2418
+ this.setConnectAuthState("isConnected", false);
2419
+ }
2368
2420
  this.lastPingTime = void 0;
2369
- this.satrtConfirm = false;
2421
+ this.setConnectAuthState("satrtConfirm", false);
2370
2422
  this.clearWebSocket();
2371
2423
  }
2372
2424
  attemptReconnect() {
@@ -2374,11 +2426,12 @@ var Socket = class {
2374
2426
  clearTimeout(this.reconnectTimer);
2375
2427
  this.reconnectTimer = void 0;
2376
2428
  }
2377
- if (this.reconnectAttempts >= this.socketConfig.maxAttempts) {
2429
+ const { maxAttempts } = this.getSocketConfig();
2430
+ if (this.reconnectAttempts >= maxAttempts) {
2378
2431
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
2379
2432
  event: "INCALL_RECONNECT_ERROR"
2380
2433
  });
2381
- this.reset();
2434
+ this.callKit.reset();
2382
2435
  this.callKit.logger.error("Maximum reconnection attempts reached", {
2383
2436
  caller: "Socket.attemptReconnect",
2384
2437
  type: "INCALL",
@@ -2394,17 +2447,17 @@ var Socket = class {
2394
2447
  event: "INCALL_RECONNECT_START"
2395
2448
  });
2396
2449
  }
2397
- this.isReconnecting = true;
2450
+ this.setConnectAuthState("isReconnecting", true);
2398
2451
  this.reconnectAttempts += 1;
2399
- const { delay } = this.socketConfig;
2452
+ const { delay } = this.getSocketConfig();
2400
2453
  this.callKit.logger.info(
2401
- `Preparing reconnection attempt ${this.reconnectAttempts}/${this.socketConfig.maxAttempts}, delay: ${delay}ms`,
2454
+ `Preparing reconnection attempt ${this.reconnectAttempts}/${maxAttempts}, delay: ${delay}ms`,
2402
2455
  {
2403
2456
  caller: "Socket.attemptReconnect",
2404
2457
  type: "INCALL",
2405
2458
  content: {
2406
2459
  reconnectAttempts: this.reconnectAttempts,
2407
- maxAttempts: this.socketConfig.maxAttempts,
2460
+ maxAttempts,
2408
2461
  delay
2409
2462
  }
2410
2463
  }
@@ -2479,6 +2532,13 @@ var CallKit = class {
2479
2532
  encryptionPassword
2480
2533
  }
2481
2534
  });
2535
+ if (this.socket.isError) {
2536
+ this.logger.warn("socket is error", {
2537
+ caller: "CallKit.login",
2538
+ content: { username, password, extra, socketError: this.socket.isError }
2539
+ });
2540
+ return;
2541
+ }
2482
2542
  try {
2483
2543
  const user = await this.api.login({
2484
2544
  userName: username,
@@ -2530,7 +2590,7 @@ var CallKit = class {
2530
2590
  try {
2531
2591
  await this.api.loginOut({ sessionId });
2532
2592
  } catch (error) {
2533
- this.logger.error(error, {
2593
+ this.logger.warn(error, {
2534
2594
  caller: "CallKit.logout",
2535
2595
  content: {
2536
2596
  errCode: ErrorCode.API_USER_LOGOUT_ERROR
@@ -2606,9 +2666,6 @@ var CallKit = class {
2606
2666
  });
2607
2667
  this.connect.unregister();
2608
2668
  }
2609
- async stop() {
2610
- await this.connect.stop();
2611
- }
2612
2669
  async hangup() {
2613
2670
  if (!this.config.check())
2614
2671
  return;
@@ -2691,11 +2748,13 @@ var CallKit = class {
2691
2748
  userStatus: status
2692
2749
  });
2693
2750
  }
2694
- async reset() {
2751
+ async _reset(config) {
2752
+ const { focus = false } = config || {};
2695
2753
  this.logger.info("reset", {
2696
2754
  caller: "CallKit.reset",
2697
2755
  content: {
2698
- connectStatus: this.connect.connectStatus
2756
+ connectStatus: this.connect.connectStatus,
2757
+ focus
2699
2758
  }
2700
2759
  });
2701
2760
  if (this.connect.isCalling()) {
@@ -2706,7 +2765,14 @@ var CallKit = class {
2706
2765
  await this.logout({ isReset: false });
2707
2766
  }
2708
2767
  await this.config.reset();
2709
- await this.socket.reset();
2768
+ await this.socket.reset({ focus });
2769
+ }
2770
+ /**
2771
+ * reset callkit
2772
+ * @description recover the callkit to the initial state
2773
+ */
2774
+ async reset() {
2775
+ await this._reset({ focus: true });
2710
2776
  }
2711
2777
  on(event, callback) {
2712
2778
  this.listener.push({