4 releases

new 0.1.3 May 4, 2025
0.1.2 Apr 30, 2025
0.1.1 Apr 30, 2025
0.1.0 Apr 25, 2025

#111 in WebSocket

Download history 113/week @ 2025-04-23 336/week @ 2025-04-30

449 downloads per month

MIT/Apache

17KB
249 lines

Next Web WebSocket

WebSocket - make everything simpler

//! Test websocket handler

use std::error::Error;
use std::sync::Arc;

use axum::extract::ws::CloseFrame;
use next_web_core::async_trait;
use next_web_dev::Singleton;
use next_web_websocket::core::handler::Result;
use next_web_websocket::core::{handler::WebSocketHandler, session::WebSocketSession};
use next_web_websocket::Message;

/// Test
#[Singleton(binds = [Self::into_websocket_handler])]
#[derive(Clone)]
pub struct TestWebSocket;

impl TestWebSocket {
    fn into_websocket_handler(self) -> Arc<dyn WebSocketHandler> {
        Arc::new(self)
    }
}

#[async_trait]
impl WebSocketHandler for TestWebSocket {
    fn paths(&self) -> Vec<&'static str> {
        vec!["/test/websocket", "/test/websocket2"]
    }

    // When the socket connection enters, this method will be entered first
    async fn on_open(&self, session: &WebSocketSession) -> Result<()> {
        println!(
            "Client remote address: {:?}, session id: {:?}",
            session.remote_address(),
            session.id()
        );
        Ok(())
    }

    /// When the client sends a message, it will enter the following method
    async fn on_message(&self, _session: &WebSocketSession, message: Message) -> Result<()> {
        if let Message::Text(msg) = message {
            println!("User message: {:?}", msg);
        }
        Ok(())
    }

    /// When an error occurs during the connection process or message transmission, the following methods will be executed
    async fn on_error(
        &self,
        _session: &WebSocketSession,
        error: Box<dyn Error + Send + Sync>,
    ) -> Result<()> {
        println!("On error: {:#}", error);
        Ok(())
    }

    /// After handling the error, close the connection and proceed to the following method
    async fn on_close(&self, session: &WebSocketSession, _close: Option<CloseFrame>) -> Result<()> {
        println!("User left: {:?}", session.id());
        Ok(())
    }
}

fn main() {
    //
}

Dependencies

~22–33MB
~634K SLoC