stormcloud-video-player 0.8.37 → 0.8.39

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.
@@ -1150,7 +1150,7 @@ function createHlsAdPlayer(contentVideo, options) {
1150
1150
  adHls.destroy();
1151
1151
  }
1152
1152
  adHls = new import_hls.default({
1153
- enableWorker: typeof globalThis !== "undefined",
1153
+ enableWorker: true,
1154
1154
  lowLatencyMode: false
1155
1155
  });
1156
1156
  adHls.loadSource(mediaFile.url);
@@ -1922,10 +1922,56 @@ function buildPlayerTopic(licenseKey, channel) {
1922
1922
  // src/utils/mqttClient.ts
1923
1923
  var import_mqtt = __toESM(require("mqtt"), 1);
1924
1924
  var LOG = "[StormcloudVideoPlayer][MQTT]";
1925
+ var KEEPALIVE_SECONDS = 30;
1926
+ var CONNECT_TIMEOUT_MS = 1e4;
1927
+ var RECONNECT_BASE_MS = 5e3;
1928
+ var RECONNECT_MAX_MS = 6e4;
1929
+ var MAX_RECONNECT_ATTEMPTS = 8;
1930
+ var COOLDOWN_MS = 10 * 6e4;
1925
1931
  var client = null;
1926
1932
  var status = "disconnected";
1927
- function initMQTTClient() {
1928
- if (client || !isMQTTEnabled()) return;
1933
+ var reconnectAttempts = 0;
1934
+ var reconnectTimer = null;
1935
+ var stopped = false;
1936
+ function clearReconnectTimer() {
1937
+ if (reconnectTimer) {
1938
+ clearTimeout(reconnectTimer);
1939
+ reconnectTimer = null;
1940
+ }
1941
+ }
1942
+ function teardownClient() {
1943
+ if (client) {
1944
+ try {
1945
+ client.removeAllListeners();
1946
+ client.end(true);
1947
+ } catch (unused) {}
1948
+ client = null;
1949
+ }
1950
+ }
1951
+ function scheduleReconnect() {
1952
+ if (stopped || !isMQTTEnabled() || reconnectTimer) return;
1953
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
1954
+ console.warn("".concat(LOG, " giving up after ").concat(reconnectAttempts, " attempts; cooling down for ").concat(COOLDOWN_MS / 6e4, "m"));
1955
+ teardownClient();
1956
+ status = "disconnected";
1957
+ reconnectTimer = setTimeout(function() {
1958
+ reconnectTimer = null;
1959
+ reconnectAttempts = 0;
1960
+ openConnection();
1961
+ }, COOLDOWN_MS);
1962
+ return;
1963
+ }
1964
+ var delay = Math.min(RECONNECT_MAX_MS, RECONNECT_BASE_MS * Math.pow(2, reconnectAttempts));
1965
+ var jitter = Math.floor(Math.random() * 1e3);
1966
+ reconnectAttempts += 1;
1967
+ reconnectTimer = setTimeout(function() {
1968
+ reconnectTimer = null;
1969
+ openConnection();
1970
+ }, delay + jitter);
1971
+ }
1972
+ function openConnection() {
1973
+ if (stopped || !isMQTTEnabled()) return;
1974
+ teardownClient();
1929
1975
  var url = buildMQTTBrokerUrl();
1930
1976
  status = "connecting";
1931
1977
  var clientId = "stormcloud-vp-".concat(Math.random().toString(36).slice(2, 9));
@@ -1934,41 +1980,48 @@ function initMQTTClient() {
1934
1980
  clientId: clientId,
1935
1981
  username: mqttConfig.username,
1936
1982
  password: mqttConfig.password,
1937
- keepalive: 60,
1983
+ keepalive: KEEPALIVE_SECONDS,
1938
1984
  clean: true,
1939
- reconnectPeriod: 5e3,
1940
- connectTimeout: 1e4,
1941
- queueQoSZero: false
1985
+ reconnectPeriod: 0,
1986
+ connectTimeout: CONNECT_TIMEOUT_MS,
1987
+ queueQoSZero: false,
1988
+ reschedulePings: true
1942
1989
  });
1943
1990
  } catch (err) {
1944
1991
  status = "error";
1945
1992
  console.warn("".concat(LOG, " connect() threw:"), err);
1993
+ scheduleReconnect();
1946
1994
  return;
1947
1995
  }
1948
1996
  client.on("connect", function() {
1949
1997
  status = "connected";
1998
+ reconnectAttempts = 0;
1999
+ clearReconnectTimer();
1950
2000
  console.info("".concat(LOG, " connected to ").concat(url));
1951
2001
  });
1952
- client.on("reconnect", function() {
1953
- status = "connecting";
1954
- console.info("".concat(LOG, " reconnecting…"));
1955
- });
1956
2002
  client.on("offline", function() {
1957
- status = "disconnected";
2003
+ if (status === "connected") status = "disconnected";
1958
2004
  console.warn("".concat(LOG, " offline"));
2005
+ scheduleReconnect();
1959
2006
  });
1960
2007
  client.on("error", function(err) {
1961
2008
  status = "error";
1962
2009
  console.warn("".concat(LOG, " error:"), err.message);
2010
+ scheduleReconnect();
1963
2011
  });
1964
2012
  client.on("close", function() {
1965
- if (status === "connected") {
1966
- status = "disconnected";
1967
- }
2013
+ if (status === "connected") status = "disconnected";
2014
+ scheduleReconnect();
1968
2015
  });
1969
2016
  }
2017
+ function initMQTTClient() {
2018
+ if (client || !isMQTTEnabled()) return;
2019
+ stopped = false;
2020
+ reconnectAttempts = 0;
2021
+ openConnection();
2022
+ }
1970
2023
  function ensureMQTTClient() {
1971
- if (isMQTTEnabled() && !client) {
2024
+ if (isMQTTEnabled() && !client && !reconnectTimer) {
1972
2025
  initMQTTClient();
1973
2026
  }
1974
2027
  }
@@ -1977,7 +2030,7 @@ function publishMQTT(topic, payload) {
1977
2030
  return false;
1978
2031
  }
1979
2032
  ensureMQTTClient();
1980
- if (!client) {
2033
+ if (!client || !client.connected) {
1981
2034
  return false;
1982
2035
  }
1983
2036
  try {
@@ -6256,7 +6309,7 @@ var HlsEngine = /*#__PURE__*/ function() {
6256
6309
  value: function setupHls() {
6257
6310
  var _this = this;
6258
6311
  this.hls = new import_hls2.default(_object_spread_props(_object_spread({
6259
- enableWorker: typeof globalThis !== "undefined",
6312
+ enableWorker: true,
6260
6313
  backBufferLength: 30,
6261
6314
  liveDurationInfinity: true,
6262
6315
  lowLatencyMode: !!this.config.lowLatencyMode,