-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add WebPlugin to keep running in background #371
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
-----BEGIN CERTIFICATE----- | ||
MIIBfTCCASOgAwIBAgIUdbC/g5dOvo7rlblM9+BRicaVJAMwCgYIKoZIzj0EAwIw | ||
FDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI0MDUwNzE0MTEzNVoXDTI0MDUyMTE0 | ||
MTEzNVowFDESMBAGA1UEAwwJbG9jYWxob3N0MFkwEwYHKoZIzj0CAQYIKoZIzj0D | ||
AQcDQgAE3BeCmoE1gzCuMBDnqMdTeWz8nvD9T2otuWlnZOtc6zL/tqv2qL38yRvg | ||
20X7/t7RE8tsMGlc41tEej/Lr11XcaNTMFEwHQYDVR0OBBYEFK7q4bBLqu3219yE | ||
W50GnETHqNY2MB8GA1UdIwQYMBaAFK7q4bBLqu3219yEW50GnETHqNY2MA8GA1Ud | ||
EwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIhAJ6w/WKnpfObuk3XVmn3yaIL | ||
SVCRgsAJVdv1cnAbmlUUAiAng/XpUyJRcdQkVvnXsw6gEUmdAUHZaZd6UhLUGiqu | ||
3A== | ||
MIIBfTCCASOgAwIBAgIUKI27VMWcE8NjR3naSwPmvjYn9lQwCgYIKoZIzj0EAwIw | ||
FDESMBAGA1UEAwwJbG9jYWxob3N0MB4XDTI0MDUyMjIyMzE0MFoXDTI0MDYwNTIy | ||
MzE0MFowFDESMBAGA1UEAwwJbG9jYWxob3N0MFkwEwYHKoZIzj0CAQYIKoZIzj0D | ||
AQcDQgAEqL2JMPpxNZnNu/GGwFCnISMBcBnHQ/aBavlt7cyVnNaa2OwhkIRYTSHw | ||
vlD6CE6thpL/EVXWcbOVw9VXWN0Y8KNTMFEwHQYDVR0OBBYEFMmrC4dORxoH7ivv | ||
qSQcVMeWOKSYMB8GA1UdIwQYMBaAFMmrC4dORxoH7ivvqSQcVMeWOKSYMA8GA1Ud | ||
EwEB/wQFMAMBAf8wCgYIKoZIzj0EAwIDSAAwRQIhAJptNIhxm05Nz5bUexxT1g0r | ||
XTwdtq+vkYODcX9KBokPAiBFcLJmsRZ8sKTHDeRNDFw3kLGDrbNqLSkBLjaTmD0G | ||
4w== | ||
-----END CERTIFICATE----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
-----BEGIN PRIVATE KEY----- | ||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgVp1Kz4Vto1yWQwbt | ||
7XhmfX4UbMDcNbrybKffjEc1mCmhRANCAATcF4KagTWDMK4wEOeox1N5bPye8P1P | ||
ai25aWdk61zrMv+2q/aovfzJG+DbRfv+3tETy2wwaVzjW0R6P8uvXVdx | ||
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQg0s/XIM5aTi33W9po | ||
uOAz0JBqR9+dd0JikOb8ZHYOn8uhRANCAASovYkw+nE1mc278YbAUKchIwFwGcdD | ||
9oFq+W3tzJWc1prY7CGQhFhNIfC+UPoITq2Gkv8RVdZxs5XD1VdY3Rjw | ||
-----END PRIVATE KEY----- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//! Module containing extra behaviour that we need when running in wasm | ||
|
||
use bevy::prelude::*; | ||
use std::sync::{Arc, RwLock}; | ||
use wasm_bindgen::{closure::Closure, JsCast, JsValue}; | ||
use web_sys::{js_sys::Array, window, Blob, Url, Worker}; | ||
|
||
pub struct WebPlugin; | ||
|
||
impl Plugin for WebPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_systems(Startup, spawn_background_worker); | ||
} | ||
} | ||
|
||
/// In wasm, the main thread gets quickly throttled by the browser when it is hidden (e.g. when the user switches tabs). | ||
/// This means that the app.update() function will not be called, because bevy's scheduler only runs `app.update()` when | ||
/// the browser's requestAnimationFrame is called. (and that happens only when the tab is visible) | ||
/// | ||
/// This is problematic because: | ||
/// - we stop sending packets so the server disconnects the client because it doesn't receive keep-alives | ||
/// - when the client reconnects, it also disconnects because it hasn't been receiving server packets | ||
/// - the internal transport buffers can overflow because they are not being emptied | ||
/// | ||
/// This solution spawns a WebWorker (a background thread) which is not throttled, and which runs | ||
/// `app.update()` at a fixed interval. This way, the client can keep sending and receiving packets, | ||
/// and updating the local World. | ||
fn spawn_background_worker(world: &mut World) { | ||
let world_ptr = Arc::new(RwLock::new(world as *mut World)); | ||
|
||
// The interval is in milliseconds. We can run app.update() infrequently when in the background | ||
let blob = Blob::new_with_str_sequence( | ||
&Array::of1(&JsValue::from_str( | ||
"setInterval(() => self.postMessage(0), 1000);", | ||
)) | ||
.unchecked_into(), | ||
) | ||
.unwrap(); | ||
|
||
let worker = Worker::new(&Url::create_object_url_with_blob(&blob).unwrap()).unwrap(); | ||
|
||
let closure = Closure::<dyn FnMut()>::new(move || { | ||
if window().unwrap().document().unwrap().hidden() { | ||
// Imitate app.update() | ||
let world = unsafe { world_ptr.write().unwrap().as_mut().unwrap() }; | ||
world.run_schedule(Main); | ||
world.clear_trackers(); | ||
} | ||
}); | ||
|
||
worker.set_onmessage(Some(closure.as_ref().unchecked_ref())); | ||
|
||
closure.forget(); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Magnifique