Fix: Generate heartbeat nonces under JS max-int size

This commit is contained in:
Kyle Simpson
2024-08-05 12:04:47 +01:00
parent 2b76eb9923
commit 2d7dc29fd6

View File

@@ -12,7 +12,7 @@ use crate::{
ConnectionInfo,
};
use flume::Receiver;
use rand::random;
use rand::{distributions::Uniform, Rng};
#[cfg(feature = "receive")]
use std::sync::Arc;
use std::time::Duration;
@@ -174,7 +174,11 @@ impl AuxNetwork {
}
async fn send_heartbeat(&mut self) -> Result<(), WsError> {
let nonce = random::<u64>();
// Discord have suddenly, mysteriously, started rejecting
// ints-as-strings. Keep JS happy here, I suppose...
const JS_MAX_INT: u64 = (1u64 << 53) - 1;
let nonce_range = Uniform::from(0..JS_MAX_INT);
let nonce = rand::thread_rng().sample(nonce_range);
self.last_heartbeat_nonce = Some(nonce);
trace!("Sent heartbeat {:?}", self.speaking);