autoconnect_ws/
session.rs
use actix_http::ws::CloseReason;
use async_trait::async_trait;
use mockall::automock;
use autoconnect_common::protocol::ServerMessage;
use crate::error::WSError;
#[automock] #[async_trait]
pub trait Session {
async fn text(&mut self, msg: ServerMessage) -> Result<(), WSError>;
async fn ping(&mut self, msg: &[u8]) -> Result<(), WSError>;
async fn pong(&mut self, msg: &[u8]) -> Result<(), WSError>;
async fn close(mut self, reason: Option<CloseReason>) -> Result<(), WSError>;
}
#[derive(Clone)]
pub struct SessionImpl {
inner: actix_ws::Session,
}
impl SessionImpl {
pub fn new(inner: actix_ws::Session) -> Self {
SessionImpl { inner }
}
}
#[async_trait]
impl Session for SessionImpl {
async fn text(&mut self, msg: ServerMessage) -> Result<(), WSError> {
Ok(self.inner.text(msg.to_json()?).await?)
}
async fn ping(&mut self, msg: &[u8]) -> Result<(), WSError> {
Ok(self.inner.ping(msg).await?)
}
async fn pong(&mut self, msg: &[u8]) -> Result<(), WSError> {
Ok(self.inner.pong(msg).await?)
}
async fn close(mut self, reason: Option<CloseReason>) -> Result<(), WSError> {
Ok(self.inner.close(reason).await?)
}
}