@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 CHANGED
@@ -61,22 +61,38 @@ interface SocketConfig {
61
61
  declare class Socket {
62
62
  private callKit;
63
63
  private ws?;
64
- private socketConfig;
65
64
  lastPingTime: any;
66
- isConnected: boolean;
67
65
  pingTimer?: ReturnType<typeof setInterval>;
68
- satrtConfirm: boolean;
66
+ /**
67
+ * @description reconnect timer
68
+ */
69
69
  private reconnectTimer?;
70
- private isReconnecting;
70
+ /**
71
+ * @description reconnect attempts
72
+ */
71
73
  private reconnectAttempts;
72
- private socketError;
74
+ /**
75
+ * @description connect auth state
76
+ * @default {
77
+ * satrtConfirm: false,
78
+ * isConnected: false,
79
+ * isReconnecting: false,
80
+ * isAuthenticated: false,
81
+ * isError: false
82
+ * }
83
+ */
84
+ private connectAuthState;
85
+ get satrtConfirm(): boolean;
86
+ get isError(): boolean;
73
87
  constructor(callKit: CallKit);
74
88
  init(): void;
89
+ private getSocketConfig;
90
+ private setConnectAuthState;
75
91
  private handleDisconnect;
76
92
  private clearWebSocket;
77
93
  private connect;
78
94
  private onOpen;
79
- private resetReconnectState;
95
+ private resetConnectState;
80
96
  private onClose;
81
97
  private onError;
82
98
  private confirmAck;
@@ -87,7 +103,9 @@ declare class Socket {
87
103
  /**
88
104
  * reset socket connection and all states
89
105
  */
90
- reset(): Promise<void>;
106
+ reset(config?: {
107
+ focus?: boolean;
108
+ }): Promise<void>;
91
109
  private attemptReconnect;
92
110
  }
93
111
 
@@ -500,7 +518,6 @@ declare class CallKit {
500
518
  refer(uri: string, options?: any): Promise<void>;
501
519
  register(): Promise<void>;
502
520
  unregister(): Promise<void>;
503
- stop(): Promise<void>;
504
521
  hangup(): Promise<void>;
505
522
  hold(): void;
506
523
  unhold(): void;
@@ -511,6 +528,13 @@ declare class CallKit {
511
528
  * @param status
512
529
  */
513
530
  setUserStatus(status: number): Promise<void>;
531
+ protected _reset(config?: {
532
+ focus?: boolean;
533
+ }): Promise<void>;
534
+ /**
535
+ * reset callkit
536
+ * @description recover the callkit to the initial state
537
+ */
514
538
  reset(): Promise<void>;
515
539
  on(event: kitEventType, callback: (...args: any[]) => void): void;
516
540
  off(event: kitEventType, callback?: (...args: any[]) => void): void;
@@ -3810,13 +3810,13 @@ var WebCall = (() => {
3810
3810
  // package.json
3811
3811
  var package_default = {
3812
3812
  name: "@koi-design/callkit",
3813
- version: "2.0.4",
3813
+ version: "2.0.5",
3814
3814
  description: "callkit",
3815
3815
  author: "koi",
3816
3816
  license: "ISC",
3817
3817
  scripts: {
3818
3818
  build: "tsup",
3819
- "build:watch": "tsup --watch",
3819
+ "build:w": "tsup --watch",
3820
3820
  dev: "vite",
3821
3821
  lint: "eslint -c ../../.eslintrc.js --ext .jsx,.js,.tsx,.ts ./package --fix",
3822
3822
  release: "tsup && node scripts/pkg.js"
@@ -19489,23 +19489,41 @@ var WebCall = (() => {
19489
19489
  var Socket = class {
19490
19490
  callKit;
19491
19491
  ws;
19492
- socketConfig;
19493
19492
  lastPingTime = void 0;
19494
- isConnected = false;
19495
19493
  pingTimer;
19496
- // Whether received start confirmation
19497
- satrtConfirm = false;
19494
+ /**
19495
+ * @description reconnect timer
19496
+ */
19498
19497
  reconnectTimer;
19499
- isReconnecting = false;
19498
+ /**
19499
+ * @description reconnect attempts
19500
+ */
19500
19501
  reconnectAttempts = 0;
19501
- socketError = false;
19502
+ /**
19503
+ * @description connect auth state
19504
+ * @default {
19505
+ * satrtConfirm: false,
19506
+ * isConnected: false,
19507
+ * isReconnecting: false,
19508
+ * isAuthenticated: false,
19509
+ * isError: false
19510
+ * }
19511
+ */
19512
+ connectAuthState = {
19513
+ satrtConfirm: false,
19514
+ isConnected: false,
19515
+ isReconnecting: false,
19516
+ isAuthenticated: false,
19517
+ isError: false
19518
+ };
19519
+ get satrtConfirm() {
19520
+ return this.connectAuthState.satrtConfirm;
19521
+ }
19522
+ get isError() {
19523
+ return this.connectAuthState.isError;
19524
+ }
19502
19525
  constructor(callKit) {
19503
19526
  this.callKit = callKit;
19504
- const { reconnect } = this.callKit.config.getConfig();
19505
- this.socketConfig = {
19506
- ...RECONNECT_CONFIG,
19507
- ...reconnect
19508
- };
19509
19527
  }
19510
19528
  init() {
19511
19529
  const { socket } = this.callKit.config.getConfig();
@@ -19518,19 +19536,29 @@ var WebCall = (() => {
19518
19536
  });
19519
19537
  this.connect(socket);
19520
19538
  }
19539
+ getSocketConfig() {
19540
+ const { reconnect } = this.callKit.config.getConfig();
19541
+ return {
19542
+ ...RECONNECT_CONFIG,
19543
+ ...reconnect
19544
+ };
19545
+ }
19546
+ setConnectAuthState(key, value) {
19547
+ if (this.connectAuthState[key] === value)
19548
+ return;
19549
+ this.connectAuthState[key] = value;
19550
+ }
19521
19551
  handleDisconnect() {
19522
- this.isConnected = false;
19523
- if (!this.callKit.config.isLogin() || !this.socketConfig.enabled) {
19524
- this.reset();
19552
+ this.setConnectAuthState("isConnected", false);
19553
+ const { enabled } = this.getSocketConfig();
19554
+ if (!this.callKit.config.isLogin() || !enabled) {
19555
+ this.callKit.reset();
19525
19556
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19526
19557
  event: "INCALL_NOT_CONNECTED"
19527
19558
  });
19528
19559
  return;
19529
19560
  }
19530
- if (this.socketError) {
19531
- return;
19532
- }
19533
- if (this.isReconnecting) {
19561
+ if (this.connectAuthState.isReconnecting) {
19534
19562
  return;
19535
19563
  }
19536
19564
  this.attemptReconnect();
@@ -19551,7 +19579,7 @@ var WebCall = (() => {
19551
19579
  });
19552
19580
  }
19553
19581
  this.ws = void 0;
19554
- this.isConnected = false;
19582
+ this.setConnectAuthState("isConnected", false);
19555
19583
  }
19556
19584
  connect(socketUrl) {
19557
19585
  if (this.ws) {
@@ -19569,11 +19597,11 @@ var WebCall = (() => {
19569
19597
  type: "INCALL",
19570
19598
  content: { ev }
19571
19599
  });
19572
- this.socketError = false;
19573
- this.isConnected = true;
19600
+ this.setConnectAuthState("isConnected", true);
19574
19601
  this.lastPingTime = Date.now();
19575
19602
  this.checkPing();
19576
- if (this.isReconnecting) {
19603
+ if (this.connectAuthState.isReconnecting) {
19604
+ this.setConnectAuthState("isReconnecting", false);
19577
19605
  this.callKit.logger.info("reconnect success", {
19578
19606
  caller: "Socket.onOpen",
19579
19607
  type: "INCALL",
@@ -19586,10 +19614,15 @@ var WebCall = (() => {
19586
19614
  event: "INCALL_RECONNECT_SUCCESS"
19587
19615
  });
19588
19616
  }
19589
- this.resetReconnectState();
19590
19617
  }
19591
- resetReconnectState() {
19592
- this.isReconnecting = false;
19618
+ resetConnectState() {
19619
+ this.connectAuthState = {
19620
+ satrtConfirm: false,
19621
+ isConnected: false,
19622
+ isReconnecting: false,
19623
+ isAuthenticated: false,
19624
+ isError: false
19625
+ };
19593
19626
  this.reconnectAttempts = 0;
19594
19627
  if (this.reconnectTimer) {
19595
19628
  clearTimeout(this.reconnectTimer);
@@ -19605,7 +19638,6 @@ var WebCall = (() => {
19605
19638
  this.handleDisconnect();
19606
19639
  }
19607
19640
  onError(ev) {
19608
- this.socketError = true;
19609
19641
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19610
19642
  event: "INCALL_CONNECT_ERROR",
19611
19643
  err: ev
@@ -19655,6 +19687,20 @@ var WebCall = (() => {
19655
19687
  this.confirmAck(data);
19656
19688
  if (data.event === SocketReceiveEvent.PONG) {
19657
19689
  this.lastPingTime = Date.now();
19690
+ this.setConnectAuthState("isAuthenticated", true);
19691
+ this.callKit.logger.info("socket onMessage pong Authenticated", {
19692
+ caller: "Socket.onMessage",
19693
+ type: "INCALL",
19694
+ content: {
19695
+ data: content,
19696
+ event: SocketReceiveEvent.PONG,
19697
+ isAuthenticated: true
19698
+ }
19699
+ });
19700
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19701
+ event: "INCALL_AUTHENTICATED",
19702
+ isAuthenticated: true
19703
+ });
19658
19704
  return;
19659
19705
  }
19660
19706
  if (data.event === SocketReceiveEvent.START_CONFIRM) {
@@ -19666,7 +19712,7 @@ var WebCall = (() => {
19666
19712
  event: SocketReceiveEvent.START_CONFIRM
19667
19713
  }
19668
19714
  });
19669
- this.satrtConfirm = true;
19715
+ this.setConnectAuthState("satrtConfirm", true);
19670
19716
  }
19671
19717
  if (data.event === SocketReceiveEvent.CALL_SUCCESS) {
19672
19718
  this.callKit.logger.info("call success", {
@@ -19780,12 +19826,10 @@ var WebCall = (() => {
19780
19826
  event: SocketReceiveEvent.CLOSE
19781
19827
  }
19782
19828
  });
19783
- this.send(SocketSendEvent.END, {
19784
- agentId: userInfo.agentId
19785
- });
19829
+ this.send(SocketSendEvent.END, { agentId: userInfo.agentId });
19786
19830
  }
19787
19831
  if (data.event === SocketReceiveEvent.ERROR) {
19788
- this.socketError = true;
19832
+ this.setConnectAuthState("isError", true);
19789
19833
  this.callKit.reset();
19790
19834
  this.callKit.logger.error(data.msg, {
19791
19835
  caller: `Socket.onMessage:${data.event}`,
@@ -19797,7 +19841,7 @@ var WebCall = (() => {
19797
19841
  });
19798
19842
  }
19799
19843
  if (data.event === SocketReceiveEvent.SESSION_ERROR) {
19800
- this.socketError = true;
19844
+ this.setConnectAuthState("isError", true);
19801
19845
  this.callKit.reset();
19802
19846
  this.callKit.logger.error(data.msg, {
19803
19847
  caller: `Socket.onMessage:${data.event}`,
@@ -19824,7 +19868,7 @@ var WebCall = (() => {
19824
19868
  this.callKit.trigger(KitEvent.SERVER_SOCKET_EVENT, data);
19825
19869
  }
19826
19870
  send(event, message) {
19827
- if (!this.isConnected) {
19871
+ if (!this.connectAuthState.isConnected) {
19828
19872
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19829
19873
  event: "INCALL_NOT_CONNECTED"
19830
19874
  });
@@ -19883,7 +19927,7 @@ var WebCall = (() => {
19883
19927
  }
19884
19928
  }
19885
19929
  ping() {
19886
- if (!this.isConnected)
19930
+ if (!this.connectAuthState.isConnected)
19887
19931
  return;
19888
19932
  this.send(SocketSendEvent.PING);
19889
19933
  this.callKit.logger.info(`socket ping`, {
@@ -19894,12 +19938,12 @@ var WebCall = (() => {
19894
19938
  }
19895
19939
  });
19896
19940
  const now = Date.now();
19897
- const { pingInterval, pingTimeout } = this.socketConfig;
19941
+ const { pingInterval, pingTimeout } = this.getSocketConfig();
19898
19942
  if (now - this.lastPingTime > pingInterval + pingTimeout) {
19899
- if (this.ws && this.isConnected) {
19943
+ if (this.ws && this.connectAuthState.isConnected) {
19900
19944
  this.ws.close(4001, "ping timeout");
19901
19945
  } else {
19902
- this.reset();
19946
+ this.callKit.reset();
19903
19947
  }
19904
19948
  this.callKit.logger.error("socket ping timeout", {
19905
19949
  caller: "Socket.ping",
@@ -19915,21 +19959,29 @@ var WebCall = (() => {
19915
19959
  clearInterval(this.pingTimer);
19916
19960
  }
19917
19961
  this.ping();
19962
+ const { pingInterval } = this.getSocketConfig();
19918
19963
  this.pingTimer = setInterval(() => {
19919
19964
  this.ping();
19920
- }, this.socketConfig.pingInterval);
19965
+ }, pingInterval);
19921
19966
  }
19922
19967
  /**
19923
19968
  * reset socket connection and all states
19924
19969
  */
19925
- async reset() {
19970
+ async reset(config) {
19971
+ const { focus = false } = config || {};
19926
19972
  if (this.pingTimer) {
19927
19973
  clearInterval(this.pingTimer);
19928
19974
  this.pingTimer = void 0;
19929
19975
  }
19930
- this.resetReconnectState();
19976
+ if (focus) {
19977
+ this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19978
+ event: "INCALL_RESET"
19979
+ });
19980
+ this.resetConnectState();
19981
+ this.setConnectAuthState("isConnected", false);
19982
+ }
19931
19983
  this.lastPingTime = void 0;
19932
- this.satrtConfirm = false;
19984
+ this.setConnectAuthState("satrtConfirm", false);
19933
19985
  this.clearWebSocket();
19934
19986
  }
19935
19987
  attemptReconnect() {
@@ -19937,11 +19989,12 @@ var WebCall = (() => {
19937
19989
  clearTimeout(this.reconnectTimer);
19938
19990
  this.reconnectTimer = void 0;
19939
19991
  }
19940
- if (this.reconnectAttempts >= this.socketConfig.maxAttempts) {
19992
+ const { maxAttempts } = this.getSocketConfig();
19993
+ if (this.reconnectAttempts >= maxAttempts) {
19941
19994
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
19942
19995
  event: "INCALL_RECONNECT_ERROR"
19943
19996
  });
19944
- this.reset();
19997
+ this.callKit.reset();
19945
19998
  this.callKit.logger.error("Maximum reconnection attempts reached", {
19946
19999
  caller: "Socket.attemptReconnect",
19947
20000
  type: "INCALL",
@@ -19957,17 +20010,17 @@ var WebCall = (() => {
19957
20010
  event: "INCALL_RECONNECT_START"
19958
20011
  });
19959
20012
  }
19960
- this.isReconnecting = true;
20013
+ this.setConnectAuthState("isReconnecting", true);
19961
20014
  this.reconnectAttempts += 1;
19962
- const { delay } = this.socketConfig;
20015
+ const { delay } = this.getSocketConfig();
19963
20016
  this.callKit.logger.info(
19964
- `Preparing reconnection attempt ${this.reconnectAttempts}/${this.socketConfig.maxAttempts}, delay: ${delay}ms`,
20017
+ `Preparing reconnection attempt ${this.reconnectAttempts}/${maxAttempts}, delay: ${delay}ms`,
19965
20018
  {
19966
20019
  caller: "Socket.attemptReconnect",
19967
20020
  type: "INCALL",
19968
20021
  content: {
19969
20022
  reconnectAttempts: this.reconnectAttempts,
19970
- maxAttempts: this.socketConfig.maxAttempts,
20023
+ maxAttempts,
19971
20024
  delay
19972
20025
  }
19973
20026
  }
@@ -20042,6 +20095,13 @@ var WebCall = (() => {
20042
20095
  encryptionPassword
20043
20096
  }
20044
20097
  });
20098
+ if (this.socket.isError) {
20099
+ this.logger.warn("socket is error", {
20100
+ caller: "CallKit.login",
20101
+ content: { username, password, extra, socketError: this.socket.isError }
20102
+ });
20103
+ return;
20104
+ }
20045
20105
  try {
20046
20106
  const user = await this.api.login({
20047
20107
  userName: username,
@@ -20093,7 +20153,7 @@ var WebCall = (() => {
20093
20153
  try {
20094
20154
  await this.api.loginOut({ sessionId });
20095
20155
  } catch (error) {
20096
- this.logger.error(error, {
20156
+ this.logger.warn(error, {
20097
20157
  caller: "CallKit.logout",
20098
20158
  content: {
20099
20159
  errCode: ErrorCode.API_USER_LOGOUT_ERROR
@@ -20169,9 +20229,6 @@ var WebCall = (() => {
20169
20229
  });
20170
20230
  this.connect.unregister();
20171
20231
  }
20172
- async stop() {
20173
- await this.connect.stop();
20174
- }
20175
20232
  async hangup() {
20176
20233
  if (!this.config.check())
20177
20234
  return;
@@ -20254,11 +20311,13 @@ var WebCall = (() => {
20254
20311
  userStatus: status
20255
20312
  });
20256
20313
  }
20257
- async reset() {
20314
+ async _reset(config) {
20315
+ const { focus = false } = config || {};
20258
20316
  this.logger.info("reset", {
20259
20317
  caller: "CallKit.reset",
20260
20318
  content: {
20261
- connectStatus: this.connect.connectStatus
20319
+ connectStatus: this.connect.connectStatus,
20320
+ focus
20262
20321
  }
20263
20322
  });
20264
20323
  if (this.connect.isCalling()) {
@@ -20269,7 +20328,14 @@ var WebCall = (() => {
20269
20328
  await this.logout({ isReset: false });
20270
20329
  }
20271
20330
  await this.config.reset();
20272
- await this.socket.reset();
20331
+ await this.socket.reset({ focus });
20332
+ }
20333
+ /**
20334
+ * reset callkit
20335
+ * @description recover the callkit to the initial state
20336
+ */
20337
+ async reset() {
20338
+ await this._reset({ focus: true });
20273
20339
  }
20274
20340
  on(event, callback) {
20275
20341
  this.listener.push({