fix: implement gateway Reconnect and InvalidSession

This commit is contained in:
kozabrada123 2024-03-10 09:02:15 +01:00
parent a3ea5c0975
commit 46dc18dd4c
5 changed files with 72 additions and 4 deletions

View File

@ -46,6 +46,8 @@ pub struct Session {
pub ready: GatewayEvent<types::GatewayReady>,
pub ready_supplemental: GatewayEvent<types::GatewayReadySupplemental>,
pub replace: GatewayEvent<types::SessionsReplace>,
pub reconnect: GatewayEvent<types::GatewayReconnect>,
pub invalid: GatewayEvent<types::GatewayInvalidSession>,
}
#[derive(Default, Debug)]

View File

@ -14,8 +14,9 @@ use super::*;
use super::{Sink, Stream};
use crate::types::{
self, AutoModerationRule, AutoModerationRuleUpdate, Channel, ChannelCreate, ChannelDelete,
ChannelUpdate, Guild, GuildRoleCreate, GuildRoleUpdate, JsonField, RoleObject, SourceUrlField,
ThreadUpdate, UpdateMessage, WebSocketEvent,
ChannelUpdate, GatewayInvalidSession, GatewayReconnect, Guild, GuildRoleCreate,
GuildRoleUpdate, JsonField, RoleObject, SourceUrlField, ThreadUpdate, UpdateMessage,
WebSocketEvent,
};
#[derive(Debug)]
@ -338,10 +339,42 @@ impl Gateway {
.unwrap();
}
GATEWAY_RECONNECT => {
todo!()
trace!("GW: Received Reconnect");
let reconnect = GatewayReconnect {};
self.events
.lock()
.await
.session
.reconnect
.notify(reconnect)
.await;
}
GATEWAY_INVALID_SESSION => {
todo!()
trace!("GW: Received Invalid Session");
let mut resumable: bool = false;
if let Some(raw_value) = gateway_payload.event_data {
if let Ok(deserialized) = serde_json::from_str(raw_value.get()) {
resumable = deserialized;
} else {
warn!("Failed to parse part of INVALID_SESSION ('{}' as bool), assuming non-resumable", raw_value.get());
}
} else {
warn!("Failed to parse part of INVALID_SESSION ('d' missing), assuming non-resumable");
}
let invalid_session = GatewayInvalidSession { resumable };
self.events
.lock()
.await
.session
.invalid
.notify(invalid_session)
.await;
}
// Starts our heartbeat
// We should have already handled this in gateway init

View File

@ -0,0 +1,17 @@
use serde::{Deserialize, Serialize};
use super::WebSocketEvent;
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
/// Your session is now invalid.
///
/// Either reauthenticate and reidentify or resume if possible.
///
/// # Reference
/// See <https://docs.discord.sex/topics/gateway-events#invalid-session>
pub struct GatewayInvalidSession {
#[serde(rename = "d")]
pub resumable: bool,
}
impl WebSocketEvent for GatewayInvalidSession {}

View File

@ -14,12 +14,14 @@ pub use hello::*;
pub use identify::*;
pub use integration::*;
pub use interaction::*;
pub use invalid_session::*;
pub use invite::*;
pub use lazy_request::*;
pub use message::*;
pub use passive_update::*;
pub use presence::*;
pub use ready::*;
pub use reconnect::*;
pub use relationship::*;
pub use request_members::*;
pub use resume::*;
@ -60,12 +62,14 @@ mod hello;
mod identify;
mod integration;
mod interaction;
mod invalid_session;
mod invite;
mod lazy_request;
mod message;
mod passive_update;
mod presence;
mod ready;
mod reconnect;
mod relationship;
mod request_members;
mod resume;

View File

@ -0,0 +1,12 @@
use serde::{Deserialize, Serialize};
use super::WebSocketEvent;
#[derive(Debug, Deserialize, Serialize, Default, Clone)]
/// "The reconnect event is dispatched when a client should reconnect to the Gateway (and resume their existing session, if they have one). This event usually occurs during deploys to migrate sessions gracefully off old hosts"
///
/// # Reference
/// See <https://docs.discord.sex/topics/gateway-events#reconnect>
pub struct GatewayReconnect {}
impl WebSocketEvent for GatewayReconnect {}