stormcloud-video-player 0.8.38 → 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.
package/lib/index.js CHANGED
@@ -1893,8 +1893,17 @@ function buildPlayerTopic(licenseKey, channel) {
1893
1893
  // src/utils/mqttClient.ts
1894
1894
  import mqtt from "mqtt";
1895
1895
  var LOG = "[StormcloudVideoPlayer][MQTT]";
1896
+ var KEEPALIVE_SECONDS = 30;
1897
+ var CONNECT_TIMEOUT_MS = 1e4;
1898
+ var RECONNECT_BASE_MS = 5e3;
1899
+ var RECONNECT_MAX_MS = 6e4;
1900
+ var MAX_RECONNECT_ATTEMPTS = 8;
1901
+ var COOLDOWN_MS = 10 * 6e4;
1896
1902
  var client = null;
1897
1903
  var status = "disconnected";
1904
+ var reconnectAttempts = 0;
1905
+ var reconnectTimer = null;
1906
+ var stopped = false;
1898
1907
  function getMQTTStatus() {
1899
1908
  return status;
1900
1909
  }
@@ -1908,8 +1917,45 @@ function configureMQTT(overrides) {
1908
1917
  applyMQTTConfig(overrides);
1909
1918
  disconnectMQTT();
1910
1919
  }
1911
- function initMQTTClient() {
1912
- if (client || !isMQTTEnabled()) return;
1920
+ function clearReconnectTimer() {
1921
+ if (reconnectTimer) {
1922
+ clearTimeout(reconnectTimer);
1923
+ reconnectTimer = null;
1924
+ }
1925
+ }
1926
+ function teardownClient() {
1927
+ if (client) {
1928
+ try {
1929
+ client.removeAllListeners();
1930
+ client.end(true);
1931
+ } catch (unused) {}
1932
+ client = null;
1933
+ }
1934
+ }
1935
+ function scheduleReconnect() {
1936
+ if (stopped || !isMQTTEnabled() || reconnectTimer) return;
1937
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
1938
+ console.warn("".concat(LOG, " giving up after ").concat(reconnectAttempts, " attempts; cooling down for ").concat(COOLDOWN_MS / 6e4, "m"));
1939
+ teardownClient();
1940
+ status = "disconnected";
1941
+ reconnectTimer = setTimeout(function() {
1942
+ reconnectTimer = null;
1943
+ reconnectAttempts = 0;
1944
+ openConnection();
1945
+ }, COOLDOWN_MS);
1946
+ return;
1947
+ }
1948
+ var delay = Math.min(RECONNECT_MAX_MS, RECONNECT_BASE_MS * Math.pow(2, reconnectAttempts));
1949
+ var jitter = Math.floor(Math.random() * 1e3);
1950
+ reconnectAttempts += 1;
1951
+ reconnectTimer = setTimeout(function() {
1952
+ reconnectTimer = null;
1953
+ openConnection();
1954
+ }, delay + jitter);
1955
+ }
1956
+ function openConnection() {
1957
+ if (stopped || !isMQTTEnabled()) return;
1958
+ teardownClient();
1913
1959
  var url = buildMQTTBrokerUrl();
1914
1960
  status = "connecting";
1915
1961
  var clientId = "stormcloud-vp-".concat(Math.random().toString(36).slice(2, 9));
@@ -1918,41 +1964,48 @@ function initMQTTClient() {
1918
1964
  clientId: clientId,
1919
1965
  username: mqttConfig.username,
1920
1966
  password: mqttConfig.password,
1921
- keepalive: 60,
1967
+ keepalive: KEEPALIVE_SECONDS,
1922
1968
  clean: true,
1923
- reconnectPeriod: 5e3,
1924
- connectTimeout: 1e4,
1925
- queueQoSZero: false
1969
+ reconnectPeriod: 0,
1970
+ connectTimeout: CONNECT_TIMEOUT_MS,
1971
+ queueQoSZero: false,
1972
+ reschedulePings: true
1926
1973
  });
1927
1974
  } catch (err) {
1928
1975
  status = "error";
1929
1976
  console.warn("".concat(LOG, " connect() threw:"), err);
1977
+ scheduleReconnect();
1930
1978
  return;
1931
1979
  }
1932
1980
  client.on("connect", function() {
1933
1981
  status = "connected";
1982
+ reconnectAttempts = 0;
1983
+ clearReconnectTimer();
1934
1984
  console.info("".concat(LOG, " connected to ").concat(url));
1935
1985
  });
1936
- client.on("reconnect", function() {
1937
- status = "connecting";
1938
- console.info("".concat(LOG, " reconnecting…"));
1939
- });
1940
1986
  client.on("offline", function() {
1941
- status = "disconnected";
1987
+ if (status === "connected") status = "disconnected";
1942
1988
  console.warn("".concat(LOG, " offline"));
1989
+ scheduleReconnect();
1943
1990
  });
1944
1991
  client.on("error", function(err) {
1945
1992
  status = "error";
1946
1993
  console.warn("".concat(LOG, " error:"), err.message);
1994
+ scheduleReconnect();
1947
1995
  });
1948
1996
  client.on("close", function() {
1949
- if (status === "connected") {
1950
- status = "disconnected";
1951
- }
1997
+ if (status === "connected") status = "disconnected";
1998
+ scheduleReconnect();
1952
1999
  });
1953
2000
  }
2001
+ function initMQTTClient() {
2002
+ if (client || !isMQTTEnabled()) return;
2003
+ stopped = false;
2004
+ reconnectAttempts = 0;
2005
+ openConnection();
2006
+ }
1954
2007
  function ensureMQTTClient() {
1955
- if (isMQTTEnabled() && !client) {
2008
+ if (isMQTTEnabled() && !client && !reconnectTimer) {
1956
2009
  initMQTTClient();
1957
2010
  }
1958
2011
  }
@@ -1961,7 +2014,7 @@ function publishMQTT(topic, payload) {
1961
2014
  return false;
1962
2015
  }
1963
2016
  ensureMQTTClient();
1964
- if (!client) {
2017
+ if (!client || !client.connected) {
1965
2018
  return false;
1966
2019
  }
1967
2020
  try {
@@ -1975,11 +2028,11 @@ function publishMQTT(topic, payload) {
1975
2028
  }
1976
2029
  }
1977
2030
  function disconnectMQTT() {
1978
- if (client) {
1979
- client.end(true);
1980
- client = null;
1981
- status = "disconnected";
1982
- }
2031
+ stopped = true;
2032
+ clearReconnectTimer();
2033
+ teardownClient();
2034
+ reconnectAttempts = 0;
2035
+ status = "disconnected";
1983
2036
  }
1984
2037
  // src/utils/vastEnvironmentSignals.ts
1985
2038
  var AIRY_ANDROID_APP_ID = "com.freeairytv.android";