next_web_websocket/core/
handler.rs

1use std::error::Error;
2
3use axum::extract::ws::{CloseFrame, Message};
4use next_web_core::async_trait;
5
6use super::session::WebSocketSession;
7
8///
9pub type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
10
11///
12#[async_trait]
13pub trait WebSocketHandler: Send + Sync {
14    ///
15    fn paths(&self) -> Vec<&'static str>;
16
17    /// When the socket connection enters, this method will be entered first
18    async fn on_open(&self, session: &WebSocketSession) -> Result<()>;
19
20    /// When the client sends a message, it will enter the following method
21    async fn on_message(&self, session: &WebSocketSession, message: Message) -> Result<()>;
22
23    /// When an error occurs during the connection process or message transmission, the following methods will be executed
24    async fn on_error(
25        &self,
26        session: &WebSocketSession,
27        error: Box<dyn Error + Send + Sync>,
28    ) -> Result<()>;
29
30    /// After handling the error, close the connection and proceed to the following method
31    async fn on_close(&self, session: &WebSocketSession, close: Option<CloseFrame>) -> Result<()>;
32}