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.
@@ -1186,7 +1186,7 @@ function createHlsAdPlayer(contentVideo, options) {
1186
1186
  adHls.destroy();
1187
1187
  }
1188
1188
  adHls = new import_hls.default({
1189
- enableWorker: typeof globalThis !== "undefined",
1189
+ enableWorker: true,
1190
1190
  lowLatencyMode: false
1191
1191
  });
1192
1192
  adHls.loadSource(mediaFile.url);
@@ -1958,10 +1958,56 @@ function buildPlayerTopic(licenseKey, channel) {
1958
1958
  // src/utils/mqttClient.ts
1959
1959
  var import_mqtt = __toESM(require("mqtt"), 1);
1960
1960
  var LOG = "[StormcloudVideoPlayer][MQTT]";
1961
+ var KEEPALIVE_SECONDS = 30;
1962
+ var CONNECT_TIMEOUT_MS = 1e4;
1963
+ var RECONNECT_BASE_MS = 5e3;
1964
+ var RECONNECT_MAX_MS = 6e4;
1965
+ var MAX_RECONNECT_ATTEMPTS = 8;
1966
+ var COOLDOWN_MS = 10 * 6e4;
1961
1967
  var client = null;
1962
1968
  var status = "disconnected";
1963
- function initMQTTClient() {
1964
- if (client || !isMQTTEnabled()) return;
1969
+ var reconnectAttempts = 0;
1970
+ var reconnectTimer = null;
1971
+ var stopped = false;
1972
+ function clearReconnectTimer() {
1973
+ if (reconnectTimer) {
1974
+ clearTimeout(reconnectTimer);
1975
+ reconnectTimer = null;
1976
+ }
1977
+ }
1978
+ function teardownClient() {
1979
+ if (client) {
1980
+ try {
1981
+ client.removeAllListeners();
1982
+ client.end(true);
1983
+ } catch (unused) {}
1984
+ client = null;
1985
+ }
1986
+ }
1987
+ function scheduleReconnect() {
1988
+ if (stopped || !isMQTTEnabled() || reconnectTimer) return;
1989
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
1990
+ console.warn("".concat(LOG, " giving up after ").concat(reconnectAttempts, " attempts; cooling down for ").concat(COOLDOWN_MS / 6e4, "m"));
1991
+ teardownClient();
1992
+ status = "disconnected";
1993
+ reconnectTimer = setTimeout(function() {
1994
+ reconnectTimer = null;
1995
+ reconnectAttempts = 0;
1996
+ openConnection();
1997
+ }, COOLDOWN_MS);
1998
+ return;
1999
+ }
2000
+ var delay = Math.min(RECONNECT_MAX_MS, RECONNECT_BASE_MS * Math.pow(2, reconnectAttempts));
2001
+ var jitter = Math.floor(Math.random() * 1e3);
2002
+ reconnectAttempts += 1;
2003
+ reconnectTimer = setTimeout(function() {
2004
+ reconnectTimer = null;
2005
+ openConnection();
2006
+ }, delay + jitter);
2007
+ }
2008
+ function openConnection() {
2009
+ if (stopped || !isMQTTEnabled()) return;
2010
+ teardownClient();
1965
2011
  var url = buildMQTTBrokerUrl();
1966
2012
  status = "connecting";
1967
2013
  var clientId = "stormcloud-vp-".concat(Math.random().toString(36).slice(2, 9));
@@ -1970,41 +2016,48 @@ function initMQTTClient() {
1970
2016
  clientId: clientId,
1971
2017
  username: mqttConfig.username,
1972
2018
  password: mqttConfig.password,
1973
- keepalive: 60,
2019
+ keepalive: KEEPALIVE_SECONDS,
1974
2020
  clean: true,
1975
- reconnectPeriod: 5e3,
1976
- connectTimeout: 1e4,
1977
- queueQoSZero: false
2021
+ reconnectPeriod: 0,
2022
+ connectTimeout: CONNECT_TIMEOUT_MS,
2023
+ queueQoSZero: false,
2024
+ reschedulePings: true
1978
2025
  });
1979
2026
  } catch (err) {
1980
2027
  status = "error";
1981
2028
  console.warn("".concat(LOG, " connect() threw:"), err);
2029
+ scheduleReconnect();
1982
2030
  return;
1983
2031
  }
1984
2032
  client.on("connect", function() {
1985
2033
  status = "connected";
2034
+ reconnectAttempts = 0;
2035
+ clearReconnectTimer();
1986
2036
  console.info("".concat(LOG, " connected to ").concat(url));
1987
2037
  });
1988
- client.on("reconnect", function() {
1989
- status = "connecting";
1990
- console.info("".concat(LOG, " reconnecting…"));
1991
- });
1992
2038
  client.on("offline", function() {
1993
- status = "disconnected";
2039
+ if (status === "connected") status = "disconnected";
1994
2040
  console.warn("".concat(LOG, " offline"));
2041
+ scheduleReconnect();
1995
2042
  });
1996
2043
  client.on("error", function(err) {
1997
2044
  status = "error";
1998
2045
  console.warn("".concat(LOG, " error:"), err.message);
2046
+ scheduleReconnect();
1999
2047
  });
2000
2048
  client.on("close", function() {
2001
- if (status === "connected") {
2002
- status = "disconnected";
2003
- }
2049
+ if (status === "connected") status = "disconnected";
2050
+ scheduleReconnect();
2004
2051
  });
2005
2052
  }
2053
+ function initMQTTClient() {
2054
+ if (client || !isMQTTEnabled()) return;
2055
+ stopped = false;
2056
+ reconnectAttempts = 0;
2057
+ openConnection();
2058
+ }
2006
2059
  function ensureMQTTClient() {
2007
- if (isMQTTEnabled() && !client) {
2060
+ if (isMQTTEnabled() && !client && !reconnectTimer) {
2008
2061
  initMQTTClient();
2009
2062
  }
2010
2063
  }
@@ -2013,7 +2066,7 @@ function publishMQTT(topic, payload) {
2013
2066
  return false;
2014
2067
  }
2015
2068
  ensureMQTTClient();
2016
- if (!client) {
2069
+ if (!client || !client.connected) {
2017
2070
  return false;
2018
2071
  }
2019
2072
  try {
@@ -6292,7 +6345,7 @@ var HlsEngine = /*#__PURE__*/ function() {
6292
6345
  value: function setupHls() {
6293
6346
  var _this = this;
6294
6347
  this.hls = new import_hls2.default(_object_spread_props(_object_spread({
6295
- enableWorker: typeof globalThis !== "undefined",
6348
+ enableWorker: true,
6296
6349
  backBufferLength: 30,
6297
6350
  liveDurationInfinity: true,
6298
6351
  lowLatencyMode: !!this.config.lowLatencyMode,