Improved fullstack reload_upon_connect timeout (#2436)
Co-authored-by: Evan Almloff <evanalmloff@gmail.com>
This commit is contained in:
1 parent
2523581cf4
commit
d3f96ec323
1 file changed
+15
-4
@@ -1,9 +1,12 @@
|
|||||||
(function () {
|
(function () {
|
||||||
|
const POLL_INTERVAL_MIN = 250;
|
||||||
|
const POLL_INTERVAL_MAX = 4000;
|
||||||
|
const POLL_INTERVAL_SCALE_FACTOR = 2;
|
||||||
|
|
||||||
var protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
var protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
|
||||||
var url = protocol + "//" + window.location.host + "/_dioxus/ws";
|
var url = protocol + "//" + window.location.host + "/_dioxus/ws";
|
||||||
var poll_interval = 8080;
|
|
||||||
|
|
||||||
var reload_upon_connect = (event) => {
|
var reload_upon_connect = (event, poll_interval) => {
|
||||||
// Firefox will send a 1001 code when the connection is closed because the page is reloaded
|
// Firefox will send a 1001 code when the connection is closed because the page is reloaded
|
||||||
// Only firefox will trigger the onclose event when the page is reloaded manually: https://stackoverflow.com/questions/10965720/should-websocket-onclose-be-triggered-by-user-navigation-or-refresh
|
// Only firefox will trigger the onclose event when the page is reloaded manually: https://stackoverflow.com/questions/10965720/should-websocket-onclose-be-triggered-by-user-navigation-or-refresh
|
||||||
// We should not reload the page in this case
|
// We should not reload the page in this case
|
||||||
@@ -13,7 +16,15 @@
|
|||||||
window.setTimeout(() => {
|
window.setTimeout(() => {
|
||||||
var ws = new WebSocket(url);
|
var ws = new WebSocket(url);
|
||||||
ws.onopen = () => window.location.reload();
|
ws.onopen = () => window.location.reload();
|
||||||
ws.onclose = reload_upon_connect;
|
ws.onclose = (event) => {
|
||||||
|
reload_upon_connect(
|
||||||
|
event,
|
||||||
|
Math.min(
|
||||||
|
POLL_INTERVAL_MAX,
|
||||||
|
poll_interval * POLL_INTERVAL_SCALE_FACTOR
|
||||||
|
)
|
||||||
|
);
|
||||||
|
};
|
||||||
}, poll_interval);
|
}, poll_interval);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -27,5 +38,5 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
ws.onclose = reload_upon_connect;
|
ws.onclose = (event) => reload_upon_connect(event, POLL_INTERVAL_MIN);
|
||||||
})();
|
})();
|
||||||
Reference in new issue
Block a user