Fix not "disconnecting" (hopefully on mobile too)

Sometimes the player would not disconnect from the Network Multiplayer
session on the back-end. Seems to work on PC browsers, not so well with
moblie browsers. This commit hopefully fixes this for mobile browsers.
This commit is contained in:
Stephen Seo 2022-04-08 12:41:00 +09:00
parent 57b782ad4d
commit 2aa4200835
2 changed files with 9 additions and 4 deletions

View file

@ -9,7 +9,7 @@ edition = "2021"
yew = "0.19"
log = "0.4.6"
wasm-logger = "0.2.0"
web-sys = { version = "0.3.56", features = ["Window", "Document", "Element", "Request", "RequestInit", "Headers", "RequestMode", "Response", "ReadableStream"] }
web-sys = { version = "0.3.56", features = ["Window", "Document", "Element", "Request", "RequestInit", "Headers", "RequestMode", "Response", "ReadableStream", "AddEventListenerOptions", "EventTarget"] }
js-sys = "0.3.56"
oorandom = "11.1.3"
wasm-bindgen = { version = "0.2.79", features = ["serde-serialize"] }

View file

@ -29,7 +29,7 @@ use std::rc::Rc;
use js_sys::{Function, Promise};
use wasm_bindgen::JsCast;
use web_sys::{Document, Response};
use web_sys::{AddEventListenerOptions, Document, Response};
use serde_json::Value as SerdeJSONValue;
@ -1238,12 +1238,17 @@ impl Component for Wrapper {
let binded_func =
outer_function.bind1(&outer_function, &resolve);
window
.add_event_listener_with_callback("pagehide", &binded_func)
.add_event_listener_with_callback_and_add_event_listener_options(
"pagehide",
&binded_func,
AddEventListenerOptions::new().capture(true).once(true)
)
.expect("Should be able to set \"pagehide\" callback");
window
.add_event_listener_with_callback(
.add_event_listener_with_callback_and_add_event_listener_options(
"beforeunload",
&binded_func,
AddEventListenerOptions::new().capture(true).once(true)
)
.expect("Should be able to set \"beforeunload\" callback");
});