diff --git a/Cargo.lock b/Cargo.lock index ce509ab..1dc6cda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -235,6 +235,7 @@ dependencies = [ "wasm-bindgen", "wasm-bindgen-futures", "wasm-bindgen-test", + "wasmtimer", "ws_stream_wasm", ] @@ -2688,6 +2689,20 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "wasmtimer" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f656cd8858a5164932d8a90f936700860976ec21eb00e0fe2aa8cab13f6b4cf" +dependencies = [ + "futures", + "js-sys", + "parking_lot", + "pin-utils", + "slab", + "wasm-bindgen", +] + [[package]] name = "web-sys" version = "0.3.66" diff --git a/Cargo.toml b/Cargo.toml index bcde77c..eff3453 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,6 @@ sqlx = { version = "0.7.3", features = [ "runtime-tokio-native-tls", "any", ], optional = true } -safina-timer = "0.1.11" rand = "0.8.5" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] @@ -64,11 +63,13 @@ tokio-tungstenite = { version = "0.20.1", features = [ ] } native-tls = "0.2.11" hostname = "0.3.1" +safina-timer = "0.1.11" [target.'cfg(target_arch = "wasm32")'.dependencies] getrandom = { version = "0.2.12", features = ["js"] } ws_stream_wasm = "0.7.4" wasm-bindgen-futures = "0.4.39" +wasmtimer = "0.2.0" [dev-dependencies] lazy_static = "1.4.0" diff --git a/src/gateway/heartbeat.rs b/src/gateway/heartbeat.rs index 1176517..0ee3b61 100644 --- a/src/gateway/heartbeat.rs +++ b/src/gateway/heartbeat.rs @@ -1,9 +1,20 @@ use futures_util::SinkExt; use log::*; -use std::time::{self, Duration, Instant}; + +#[cfg(not(target_arch = "wasm32"))] +use std::time::Instant; +#[cfg(target_arch = "wasm32")] +use wasmtimer::std::Instant; + +#[cfg(not(target_arch = "wasm32"))] +use safina_timer::sleep_until; +#[cfg(target_arch = "wasm32")] +use wasmtimer::tokio::sleep_until; + +use std::time::Duration; + use tokio::sync::mpsc::{Receiver, Sender}; -use safina_timer::sleep_until; #[cfg(not(target_arch = "wasm32"))] use tokio::task; @@ -57,7 +68,7 @@ impl HeartbeatHandler { mut receive: Receiver, mut kill_receive: tokio::sync::broadcast::Receiver<()>, ) { - let mut last_heartbeat_timestamp: Instant = time::Instant::now(); + let mut last_heartbeat_timestamp: Instant = Instant::now(); let mut last_heartbeat_acknowledged = true; let mut last_seq_number: Option = None; @@ -123,7 +134,7 @@ impl HeartbeatHandler { break; } - last_heartbeat_timestamp = time::Instant::now(); + last_heartbeat_timestamp = Instant::now(); last_heartbeat_acknowledged = false; } }