Harden embed live refresh with reconnect backoff and poll fallback.
Document Nginx SSE proxy settings for long-lived live streams. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
/**
|
||||
* embed 壳:SSE 收到后台 tick 后拉 JSON 快照更新 DOM,切换 tab 不再重复请求 HTML.
|
||||
* 另有可见性恢复 + SSE 断开时的轮询兜底,避免经 Nginx 几分钟后停刷.
|
||||
*/
|
||||
(function (global) {
|
||||
let liveEventSource = null;
|
||||
@@ -7,6 +8,8 @@
|
||||
let localLiveVersion = -1;
|
||||
let sseConnected = false;
|
||||
let refreshTimer = null;
|
||||
let fallbackTimer = null;
|
||||
let reconnectAttempt = 0;
|
||||
|
||||
function isEmbedShell() {
|
||||
return document.body && document.body.getAttribute("data-embed-shell") === "1";
|
||||
@@ -16,7 +19,7 @@
|
||||
if (global.InstanceEmbed && typeof global.InstanceEmbed.getTab === "function") {
|
||||
return global.InstanceEmbed.getTab();
|
||||
}
|
||||
return document.body.getAttribute("data-page") || "trade";
|
||||
return document.body.getAttribute("data-page") || "options";
|
||||
}
|
||||
|
||||
function refreshTabData(tab, opts) {
|
||||
@@ -30,7 +33,9 @@
|
||||
if (tab === "options" && global.OptionsPanelLive && typeof global.OptionsPanelLive.refreshSoft === "function") {
|
||||
global.OptionsPanelLive.refreshSoft(options);
|
||||
}
|
||||
// 数据看板自有 SSE + 快照,不跟 embed live tick 重拉.
|
||||
if (tab === "hedge_plan" && global.HedgePlanLive && typeof global.HedgePlanLive.refreshSoft === "function") {
|
||||
global.HedgePlanLive.refreshSoft(options);
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleRefresh(opts) {
|
||||
@@ -76,23 +81,42 @@
|
||||
liveEventSource.addEventListener("live", function (ev) {
|
||||
try {
|
||||
onLiveEvent(JSON.parse(ev.data || "{}"));
|
||||
reconnectAttempt = 0;
|
||||
} catch (_) {}
|
||||
});
|
||||
liveEventSource.onopen = function () {
|
||||
sseConnected = true;
|
||||
reconnectAttempt = 0;
|
||||
};
|
||||
liveEventSource.onerror = function () {
|
||||
sseConnected = false;
|
||||
closeLiveStream();
|
||||
reconnectAttempt += 1;
|
||||
var delay = Math.min(30000, 3000 + reconnectAttempt * 2000);
|
||||
liveReconnectTimer = setTimeout(function () {
|
||||
connectLiveStream();
|
||||
}, 8000);
|
||||
}, delay);
|
||||
};
|
||||
}
|
||||
|
||||
function ensureFallbackPoll() {
|
||||
if (fallbackTimer) return;
|
||||
// SSE 正常时也做低频兜底;断开时依赖它持续刷新
|
||||
fallbackTimer = setInterval(function () {
|
||||
if (document.hidden) return;
|
||||
scheduleRefresh({ force: !sseConnected });
|
||||
}, sseConnected ? 20000 : 8000);
|
||||
}
|
||||
|
||||
function startLive() {
|
||||
if (!isEmbedShell()) return;
|
||||
connectLiveStream();
|
||||
ensureFallbackPoll();
|
||||
document.addEventListener("visibilitychange", function () {
|
||||
if (document.hidden) return;
|
||||
if (!sseConnected) connectLiveStream();
|
||||
scheduleRefresh({ force: true });
|
||||
});
|
||||
}
|
||||
|
||||
global.InstanceLive = {
|
||||
|
||||
@@ -145,7 +145,7 @@ const ORDER_ENTRY_MODEL_CODE_TO_CATEGORY = {{ entry_model_code_to_category | toj
|
||||
window.__INSTANCE_DISPLAY__ = {{ display | tojson }};
|
||||
</script>
|
||||
<script src="/static/instance_settings_prefs.js?v=21"></script>
|
||||
<script src="/static/instance_live.js?v=6"></script>
|
||||
<script src="/static/instance_live.js?v=7"></script>
|
||||
<script src="/static/instance_embed.js?v=31"></script>
|
||||
<script src="/static/instance_mobile_nav.js?v=2"></script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user