@koi-design/callkit 2.2.0-beta.2 → 2.2.0-beta.4

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
@@ -4805,7 +4805,7 @@ declare class Socket {
4805
4805
  private callKit;
4806
4806
  private ws?;
4807
4807
  lastPingTime: any;
4808
- pingTimer?: ReturnType<typeof setInterval>;
4808
+ private heartbeatManager;
4809
4809
  /**
4810
4810
  * @description reconnect timer
4811
4811
  */
@@ -4852,6 +4852,10 @@ declare class Socket {
4852
4852
  reset(config?: {
4853
4853
  force?: boolean;
4854
4854
  }): Promise<void>;
4855
+ /**
4856
+ * Destroy the heartbeat manager
4857
+ */
4858
+ destroyHeartbeat(): void;
4855
4859
  private attemptReconnect;
4856
4860
  }
4857
4861
 
@@ -1736,7 +1736,7 @@ var WebCall = (() => {
1736
1736
  var require_follow_redirects = __commonJS({
1737
1737
  "../../node_modules/.pnpm/follow-redirects@1.15.9/node_modules/follow-redirects/index.js"(exports, module) {
1738
1738
  var url = __require("url");
1739
- var URL = url.URL;
1739
+ var URL2 = url.URL;
1740
1740
  var http = __require("http");
1741
1741
  var https = __require("https");
1742
1742
  var Writable = __require("stream").Writable;
@@ -1752,7 +1752,7 @@ var WebCall = (() => {
1752
1752
  })();
1753
1753
  var useNativeURL = false;
1754
1754
  try {
1755
- assert(new URL(""));
1755
+ assert(new URL2(""));
1756
1756
  } catch (error) {
1757
1757
  useNativeURL = error.code === "ERR_INVALID_URL";
1758
1758
  }
@@ -2132,7 +2132,7 @@ var WebCall = (() => {
2132
2132
  function parseUrl(input) {
2133
2133
  var parsed;
2134
2134
  if (useNativeURL) {
2135
- parsed = new URL(input);
2135
+ parsed = new URL2(input);
2136
2136
  } else {
2137
2137
  parsed = validateUrl(url.parse(input));
2138
2138
  if (!isString(parsed.protocol)) {
@@ -2142,7 +2142,7 @@ var WebCall = (() => {
2142
2142
  return parsed;
2143
2143
  }
2144
2144
  function resolveUrl(relative, base) {
2145
- return useNativeURL ? new URL(relative, base) : parseUrl(url.resolve(base, relative));
2145
+ return useNativeURL ? new URL2(relative, base) : parseUrl(url.resolve(base, relative));
2146
2146
  }
2147
2147
  function validateUrl(input) {
2148
2148
  if (/^\[/.test(input.hostname) && !/^\[[:0-9a-f]+\]$/i.test(input.hostname)) {
@@ -2221,7 +2221,7 @@ var WebCall = (() => {
2221
2221
  return typeof value === "object" && "length" in value;
2222
2222
  }
2223
2223
  function isURL(value) {
2224
- return URL && value instanceof URL;
2224
+ return URL2 && value instanceof URL2;
2225
2225
  }
2226
2226
  module.exports = wrap({ http, https });
2227
2227
  module.exports.wrap = wrap;
@@ -3883,7 +3883,7 @@ var WebCall = (() => {
3883
3883
  // package.json
3884
3884
  var package_default = {
3885
3885
  name: "@koi-design/callkit",
3886
- version: "2.2.0-beta.2",
3886
+ version: "2.2.0-beta.4",
3887
3887
  description: "callkit",
3888
3888
  author: "koi",
3889
3889
  license: "ISC",
@@ -19867,12 +19867,152 @@ ${log}` : log;
19867
19867
  }
19868
19868
  };
19869
19869
 
19870
+ // core/heartbeat-worker.ts
19871
+ var workerCode = `
19872
+ let timer = null;
19873
+ let interval = 30000;
19874
+
19875
+ self.onmessage = function(e) {
19876
+ const { type, interval: newInterval } = e.data;
19877
+
19878
+ if (type === 'start') {
19879
+ if (timer) {
19880
+ clearInterval(timer);
19881
+ }
19882
+ interval = newInterval || interval;
19883
+ timer = setInterval(() => {
19884
+ self.postMessage({ type: 'tick' });
19885
+ }, interval);
19886
+ }
19887
+
19888
+ if (type === 'stop') {
19889
+ if (timer) {
19890
+ clearInterval(timer);
19891
+ timer = null;
19892
+ }
19893
+ }
19894
+
19895
+ if (type === 'updateInterval') {
19896
+ interval = newInterval;
19897
+ if (timer) {
19898
+ clearInterval(timer);
19899
+ timer = setInterval(() => {
19900
+ self.postMessage({ type: 'tick' });
19901
+ }, interval);
19902
+ }
19903
+ }
19904
+ };
19905
+ `;
19906
+ function createHeartbeatWorker() {
19907
+ try {
19908
+ const blob = new Blob([workerCode], { type: "application/javascript" });
19909
+ const workerUrl = URL.createObjectURL(blob);
19910
+ const worker = new Worker(workerUrl);
19911
+ URL.revokeObjectURL(workerUrl);
19912
+ return worker;
19913
+ } catch {
19914
+ return null;
19915
+ }
19916
+ }
19917
+ var HeartbeatManager = class {
19918
+ worker = null;
19919
+ fallbackTimer = null;
19920
+ interval = 3e4;
19921
+ onTick = null;
19922
+ isRunning = false;
19923
+ constructor() {
19924
+ this.worker = createHeartbeatWorker();
19925
+ if (this.worker) {
19926
+ this.worker.onmessage = (e) => {
19927
+ if (e.data.type === "tick" && this.onTick) {
19928
+ this.onTick();
19929
+ }
19930
+ };
19931
+ }
19932
+ }
19933
+ /**
19934
+ * Start the heartbeat
19935
+ * @param interval - Interval in milliseconds
19936
+ * @param onTick - Callback function to execute on each tick
19937
+ */
19938
+ start(interval, onTick) {
19939
+ this.stop();
19940
+ this.interval = interval;
19941
+ this.onTick = onTick;
19942
+ this.isRunning = true;
19943
+ if (this.worker) {
19944
+ this.worker.postMessage({
19945
+ type: "start",
19946
+ interval
19947
+ });
19948
+ } else {
19949
+ this.fallbackTimer = setInterval(() => {
19950
+ if (this.onTick) {
19951
+ this.onTick();
19952
+ }
19953
+ }, interval);
19954
+ }
19955
+ }
19956
+ /**
19957
+ * Stop the heartbeat
19958
+ */
19959
+ stop() {
19960
+ this.isRunning = false;
19961
+ this.onTick = null;
19962
+ if (this.worker) {
19963
+ this.worker.postMessage({ type: "stop" });
19964
+ }
19965
+ if (this.fallbackTimer) {
19966
+ clearInterval(this.fallbackTimer);
19967
+ this.fallbackTimer = null;
19968
+ }
19969
+ }
19970
+ /**
19971
+ * Update the heartbeat interval
19972
+ * @param interval - New interval in milliseconds
19973
+ */
19974
+ updateInterval(interval) {
19975
+ this.interval = interval;
19976
+ if (!this.isRunning)
19977
+ return;
19978
+ if (this.worker) {
19979
+ this.worker.postMessage({
19980
+ type: "updateInterval",
19981
+ interval
19982
+ });
19983
+ } else if (this.fallbackTimer && this.onTick) {
19984
+ clearInterval(this.fallbackTimer);
19985
+ this.fallbackTimer = setInterval(() => {
19986
+ if (this.onTick) {
19987
+ this.onTick();
19988
+ }
19989
+ }, interval);
19990
+ }
19991
+ }
19992
+ /**
19993
+ * Destroy the heartbeat manager and release resources
19994
+ */
19995
+ destroy() {
19996
+ this.stop();
19997
+ if (this.worker) {
19998
+ this.worker.terminate();
19999
+ this.worker = null;
20000
+ }
20001
+ }
20002
+ /**
20003
+ * Check if using Web Worker
20004
+ */
20005
+ isUsingWorker() {
20006
+ return this.worker !== null;
20007
+ }
20008
+ };
20009
+
19870
20010
  // core/socket.ts
19871
20011
  var Socket = class {
19872
20012
  callKit;
19873
20013
  ws;
19874
20014
  lastPingTime = void 0;
19875
- pingTimer;
20015
+ heartbeatManager;
19876
20016
  /**
19877
20017
  * @description reconnect timer
19878
20018
  */
@@ -19905,6 +20045,7 @@ ${log}` : log;
19905
20045
  }
19906
20046
  constructor(callKit) {
19907
20047
  this.callKit = callKit;
20048
+ this.heartbeatManager = new HeartbeatManager();
19908
20049
  }
19909
20050
  get reconnectConfig() {
19910
20051
  return this.callKit.config.getReconnectConfig("incall");
@@ -20266,22 +20407,27 @@ ${log}` : log;
20266
20407
  }
20267
20408
  }
20268
20409
  checkPing() {
20269
- if (this.pingTimer) {
20270
- clearInterval(this.pingTimer);
20271
- }
20272
- this.pingTimer = setInterval(() => {
20410
+ this.heartbeatManager.start(this.pingInterval, () => {
20273
20411
  this.ping();
20274
- }, this.pingInterval);
20412
+ });
20413
+ this.callKit.logger.info(
20414
+ `Heartbeat started with Worker: ${this.heartbeatManager.isUsingWorker()}`,
20415
+ {
20416
+ caller: "Socket.checkPing",
20417
+ type: "INCALL",
20418
+ content: {
20419
+ pingInterval: this.pingInterval,
20420
+ usingWorker: this.heartbeatManager.isUsingWorker()
20421
+ }
20422
+ }
20423
+ );
20275
20424
  }
20276
20425
  /**
20277
20426
  * reset socket connection and all states
20278
20427
  */
20279
20428
  async reset(config) {
20280
20429
  const { force = false } = config || {};
20281
- if (this.pingTimer) {
20282
- clearInterval(this.pingTimer);
20283
- this.pingTimer = void 0;
20284
- }
20430
+ this.heartbeatManager.stop();
20285
20431
  if (force) {
20286
20432
  this.callKit.trigger(KitEvent.INCALL_CONNECT_EVENT, {
20287
20433
  event: "INCALL_RESET"
@@ -20292,6 +20438,12 @@ ${log}` : log;
20292
20438
  this.setConnectAuthState("startConfirm", false);
20293
20439
  this.clearWebSocket();
20294
20440
  }
20441
+ /**
20442
+ * Destroy the heartbeat manager
20443
+ */
20444
+ destroyHeartbeat() {
20445
+ this.heartbeatManager.destroy();
20446
+ }
20295
20447
  attemptReconnect() {
20296
20448
  if (this.reconnectTimer) {
20297
20449
  clearTimeout(this.reconnectTimer);
@@ -20435,6 +20587,7 @@ ${log}` : log;
20435
20587
  password: encryptionPassword,
20436
20588
  timestamp: Date.now()
20437
20589
  });
20590
+ console.log("user", user);
20438
20591
  if (user) {
20439
20592
  this.config.setConfig("userInfo", {
20440
20593
  wsUrl: `wss://${user.wsUrl}`,
@@ -20451,6 +20604,7 @@ ${log}` : log;
20451
20604
  iceInfo: user.iceInfo,
20452
20605
  iceGatheringTimeout: user.iceGatheringTimeout,
20453
20606
  logGather: user.logGather,
20607
+ keepaliveInterval: user.keepaliveInterval,
20454
20608
  // encryptionType is in extra
20455
20609
  ...extra
20456
20610
  });