stormcloud-video-player 0.8.38 → 0.8.40

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.
@@ -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,57 @@ 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
+ // Disable MQTT.js internal reconnect; we manage reconnection ourselves.
1986
+ reconnectPeriod: 0,
1987
+ connectTimeout: CONNECT_TIMEOUT_MS,
1988
+ queueQoSZero: false,
1989
+ reschedulePings: true,
1990
+ // Root-cause fix for the Tizen 4.0 "Keepalive timeout" loop: MQTT.js's
1991
+ // default timerVariant "auto" runs the keepalive interval inside a
1992
+ // Blob-URL Web Worker (via worker-timers). On Samsung Tizen 4.0 (old
1993
+ // WebKit) served from a file:// origin that worker can't tick reliably,
1994
+ // so the keepalive counter trips onKeepaliveTimeout almost immediately
1995
+ // after connect. Force the native setInterval timer instead; the main
1996
+ // thread is free because video is decoded by the native AVPlay pipeline.
1997
+ timerVariant: "native"
1942
1998
  });
1943
1999
  } catch (err) {
1944
2000
  status = "error";
1945
2001
  console.warn("".concat(LOG, " connect() threw:"), err);
2002
+ scheduleReconnect();
1946
2003
  return;
1947
2004
  }
1948
2005
  client.on("connect", function() {
1949
2006
  status = "connected";
2007
+ reconnectAttempts = 0;
2008
+ clearReconnectTimer();
1950
2009
  console.info("".concat(LOG, " connected to ").concat(url));
1951
2010
  });
1952
- client.on("reconnect", function() {
1953
- status = "connecting";
1954
- console.info("".concat(LOG, " reconnecting…"));
1955
- });
1956
2011
  client.on("offline", function() {
1957
- status = "disconnected";
2012
+ if (status === "connected") status = "disconnected";
1958
2013
  console.warn("".concat(LOG, " offline"));
2014
+ scheduleReconnect();
1959
2015
  });
1960
2016
  client.on("error", function(err) {
1961
2017
  status = "error";
1962
2018
  console.warn("".concat(LOG, " error:"), err.message);
2019
+ scheduleReconnect();
1963
2020
  });
1964
2021
  client.on("close", function() {
1965
- if (status === "connected") {
1966
- status = "disconnected";
1967
- }
2022
+ if (status === "connected") status = "disconnected";
2023
+ scheduleReconnect();
1968
2024
  });
1969
2025
  }
2026
+ function initMQTTClient() {
2027
+ if (client || !isMQTTEnabled()) return;
2028
+ stopped = false;
2029
+ reconnectAttempts = 0;
2030
+ openConnection();
2031
+ }
1970
2032
  function ensureMQTTClient() {
1971
- if (isMQTTEnabled() && !client) {
2033
+ if (isMQTTEnabled() && !client && !reconnectTimer) {
1972
2034
  initMQTTClient();
1973
2035
  }
1974
2036
  }
@@ -1977,7 +2039,7 @@ function publishMQTT(topic, payload) {
1977
2039
  return false;
1978
2040
  }
1979
2041
  ensureMQTTClient();
1980
- if (!client) {
2042
+ if (!client || !client.connected) {
1981
2043
  return false;
1982
2044
  }
1983
2045
  try {