From 2aa420083512225ac0af63c0e73a20e5b1f897c0 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Fri, 8 Apr 2022 12:41:00 +0900 Subject: [PATCH] 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. --- front_end/Cargo.toml | 2 +- front_end/src/yew_components.rs | 11 ++++++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/front_end/Cargo.toml b/front_end/Cargo.toml index 0166b84..a4094ca 100644 --- a/front_end/Cargo.toml +++ b/front_end/Cargo.toml @@ -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"] } diff --git a/front_end/src/yew_components.rs b/front_end/src/yew_components.rs index 03f6b67..defc412 100644 --- a/front_end/src/yew_components.rs +++ b/front_end/src/yew_components.rs @@ -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"); });