Struct twitch_message::PingTracker
source · pub struct PingTracker { /* private fields */ }
Available on crate feature
ping
only.Expand description
A simple type to track PINGs and if you should PONG
This requires the ping
feature to be enabled
If either sync
or parking_lot
features are also enabled, then this type is safe to send to other threads
use twitch_message::PingTracker;
// create a new tracker, the `threshold` is used to determine when a connection is dead/stale.
let pt = PingTracker::new(std::time::Duration::from_secs(10 * 60));
// in some loop
// if its been a while (such as if you have a way to keep track of time)
if pt.probably_timed_out() {
// we should reconnect
return Err("timed out".into());
}
// this might block for a while
let msg = read_message();
// update the tracker
pt.update(&msg);
// check to see if you should reply.
// this returns a message you can write to your sink
if let Some(pong) = pt.should_pong() {
io_sink.encode_msg(pong)?;
}
Implementations§
source§impl PingTracker
impl PingTracker
sourcepub const fn new(threshold: Duration) -> Self
pub const fn new(threshold: Duration) -> Self
Create a new PingTracker
with a ‘timeout’ duration
sourcepub fn should_pong(&self) -> Option<Pong<'static>>
pub fn should_pong(&self) -> Option<Pong<'static>>
Determines whether you should PONG
This returns the message you should encode
sourcepub fn probably_timed_out(&self) -> bool
pub fn probably_timed_out(&self) -> bool
Determines if the timeout threshold has been reached