Measure multiplayer relay ping
This commit is contained in:
1 parent
4834adca7b
commit
ef148087f4
2 files changed
+81
-1
No files matched your search
+15
-1
@@ -19,7 +19,7 @@
|
||||
// {"t":"set_ai","side":2,"ai":true} -> the host marks an empty seat as AI, or clears it again
|
||||
// {"t":"leave"} -> a player leaves; a started human seat becomes AI-controlled while anyone is connected
|
||||
//
|
||||
// Server messages include: hello, rooms, room, start, turn, sum, chat, closed, error.
|
||||
// Server messages include: hello, rooms, room, start, turn, sum, chat, closed, error, pong.
|
||||
|
||||
use crate::AppState;
|
||||
use axum::{
|
||||
@@ -276,6 +276,9 @@ impl Netplay {
|
||||
self.leave_room(conn, &code, false);
|
||||
}
|
||||
}
|
||||
"ping" => {
|
||||
self.send(conn, json!({"t":"pong","c":msg.get("c").cloned().unwrap_or(Value::Null)}));
|
||||
}
|
||||
_ => self.error(conn, "unknown message"),
|
||||
}
|
||||
Vec::new()
|
||||
@@ -988,4 +991,15 @@ mod tests {
|
||||
assert_eq!(team_red["side"].as_u64(), Some(0));
|
||||
assert!(rx2.try_recv().is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_relay_answers_a_ping_with_the_same_correlation_number() {
|
||||
let mut net = Netplay::new();
|
||||
let (tx, mut rx) = mpsc::unbounded_channel();
|
||||
let red = net.connect(tx, Some("red".into()), None);
|
||||
net.handle(red, json!({"t":"ping","c":7}));
|
||||
|
||||
let pong = until(&mut rx, "pong");
|
||||
assert_eq!(pong["c"].as_u64(), Some(7));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user