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.
@@ -1871,10 +1871,56 @@ function buildPlayerTopic(licenseKey, channel) {
1871
1871
  // src/utils/mqttClient.ts
1872
1872
  var import_mqtt = __toESM(require("mqtt"), 1);
1873
1873
  var LOG = "[StormcloudVideoPlayer][MQTT]";
1874
+ var KEEPALIVE_SECONDS = 30;
1875
+ var CONNECT_TIMEOUT_MS = 1e4;
1876
+ var RECONNECT_BASE_MS = 5e3;
1877
+ var RECONNECT_MAX_MS = 6e4;
1878
+ var MAX_RECONNECT_ATTEMPTS = 8;
1879
+ var COOLDOWN_MS = 10 * 6e4;
1874
1880
  var client = null;
1875
1881
  var status = "disconnected";
1876
- function initMQTTClient() {
1877
- if (client || !isMQTTEnabled()) return;
1882
+ var reconnectAttempts = 0;
1883
+ var reconnectTimer = null;
1884
+ var stopped = false;
1885
+ function clearReconnectTimer() {
1886
+ if (reconnectTimer) {
1887
+ clearTimeout(reconnectTimer);
1888
+ reconnectTimer = null;
1889
+ }
1890
+ }
1891
+ function teardownClient() {
1892
+ if (client) {
1893
+ try {
1894
+ client.removeAllListeners();
1895
+ client.end(true);
1896
+ } catch (unused) {}
1897
+ client = null;
1898
+ }
1899
+ }
1900
+ function scheduleReconnect() {
1901
+ if (stopped || !isMQTTEnabled() || reconnectTimer) return;
1902
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
1903
+ console.warn("".concat(LOG, " giving up after ").concat(reconnectAttempts, " attempts; cooling down for ").concat(COOLDOWN_MS / 6e4, "m"));
1904
+ teardownClient();
1905
+ status = "disconnected";
1906
+ reconnectTimer = setTimeout(function() {
1907
+ reconnectTimer = null;
1908
+ reconnectAttempts = 0;
1909
+ openConnection();
1910
+ }, COOLDOWN_MS);
1911
+ return;
1912
+ }
1913
+ var delay = Math.min(RECONNECT_MAX_MS, RECONNECT_BASE_MS * Math.pow(2, reconnectAttempts));
1914
+ var jitter = Math.floor(Math.random() * 1e3);
1915
+ reconnectAttempts += 1;
1916
+ reconnectTimer = setTimeout(function() {
1917
+ reconnectTimer = null;
1918
+ openConnection();
1919
+ }, delay + jitter);
1920
+ }
1921
+ function openConnection() {
1922
+ if (stopped || !isMQTTEnabled()) return;
1923
+ teardownClient();
1878
1924
  var url = buildMQTTBrokerUrl();
1879
1925
  status = "connecting";
1880
1926
  var clientId = "stormcloud-vp-".concat(Math.random().toString(36).slice(2, 9));
@@ -1883,41 +1929,57 @@ function initMQTTClient() {
1883
1929
  clientId: clientId,
1884
1930
  username: mqttConfig.username,
1885
1931
  password: mqttConfig.password,
1886
- keepalive: 60,
1932
+ keepalive: KEEPALIVE_SECONDS,
1887
1933
  clean: true,
1888
- reconnectPeriod: 5e3,
1889
- connectTimeout: 1e4,
1890
- queueQoSZero: false
1934
+ // Disable MQTT.js internal reconnect; we manage reconnection ourselves.
1935
+ reconnectPeriod: 0,
1936
+ connectTimeout: CONNECT_TIMEOUT_MS,
1937
+ queueQoSZero: false,
1938
+ reschedulePings: true,
1939
+ // Root-cause fix for the Tizen 4.0 "Keepalive timeout" loop: MQTT.js's
1940
+ // default timerVariant "auto" runs the keepalive interval inside a
1941
+ // Blob-URL Web Worker (via worker-timers). On Samsung Tizen 4.0 (old
1942
+ // WebKit) served from a file:// origin that worker can't tick reliably,
1943
+ // so the keepalive counter trips onKeepaliveTimeout almost immediately
1944
+ // after connect. Force the native setInterval timer instead; the main
1945
+ // thread is free because video is decoded by the native AVPlay pipeline.
1946
+ timerVariant: "native"
1891
1947
  });
1892
1948
  } catch (err) {
1893
1949
  status = "error";
1894
1950
  console.warn("".concat(LOG, " connect() threw:"), err);
1951
+ scheduleReconnect();
1895
1952
  return;
1896
1953
  }
1897
1954
  client.on("connect", function() {
1898
1955
  status = "connected";
1956
+ reconnectAttempts = 0;
1957
+ clearReconnectTimer();
1899
1958
  console.info("".concat(LOG, " connected to ").concat(url));
1900
1959
  });
1901
- client.on("reconnect", function() {
1902
- status = "connecting";
1903
- console.info("".concat(LOG, " reconnecting…"));
1904
- });
1905
1960
  client.on("offline", function() {
1906
- status = "disconnected";
1961
+ if (status === "connected") status = "disconnected";
1907
1962
  console.warn("".concat(LOG, " offline"));
1963
+ scheduleReconnect();
1908
1964
  });
1909
1965
  client.on("error", function(err) {
1910
1966
  status = "error";
1911
1967
  console.warn("".concat(LOG, " error:"), err.message);
1968
+ scheduleReconnect();
1912
1969
  });
1913
1970
  client.on("close", function() {
1914
- if (status === "connected") {
1915
- status = "disconnected";
1916
- }
1971
+ if (status === "connected") status = "disconnected";
1972
+ scheduleReconnect();
1917
1973
  });
1918
1974
  }
1975
+ function initMQTTClient() {
1976
+ if (client || !isMQTTEnabled()) return;
1977
+ stopped = false;
1978
+ reconnectAttempts = 0;
1979
+ openConnection();
1980
+ }
1919
1981
  function ensureMQTTClient() {
1920
- if (isMQTTEnabled() && !client) {
1982
+ if (isMQTTEnabled() && !client && !reconnectTimer) {
1921
1983
  initMQTTClient();
1922
1984
  }
1923
1985
  }
@@ -1926,7 +1988,7 @@ function publishMQTT(topic, payload) {
1926
1988
  return false;
1927
1989
  }
1928
1990
  ensureMQTTClient();
1929
- if (!client) {
1991
+ if (!client || !client.connected) {
1930
1992
  return false;
1931
1993
  }
1932
1994
  try {