tokio_websocket_server 0.1.0

A robust WebSocket server implementation with TLS support built on Tokio
Documentation
pub mod socket;

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn start_websocket() {
        use crate::socket::{WebSocketMessage, WebsocketServer};
        // Create WebSocket server
        let ws_server = WebsocketServer::new("0.0.0.0".to_string(), "5000".to_string(), None, None);

        // Get a clone of the server for use in the main application
        let ws_server_clone = ws_server.clone();

        // Start the WebSocket server and get the message receiver
        let mut ws_message_receiver = ws_server.start().await;

        // Handle incoming WebSocket messages in a separate task
        // Remove this if you want to make the task blocking
        tokio::spawn(async move { 
            while let Some((client_id, message)) = ws_message_receiver.recv().await {
                match message {
                    WebSocketMessage::Text(text) => {
                        tracing::info!("Received text message from {}: {}", client_id, text);

                        // Perform business logic

                        // Echo the message back to the sender
                        let response = WebSocketMessage::Text(format!("Echo: {}", text));

                        if let Err(e) = ws_server_clone
                            .send_to_client(client_id.clone(), response)
                            .await
                        {
                            tracing::error!("Failed to respond to client {}: {}", client_id, e);
                        }
                    }
                    WebSocketMessage::Binary(data) => {
                        tracing::info!(
                            "Received binary message from {}: {} bytes",
                            client_id,
                            data.len()
                        );

                        // Perform business logic

                        // Echo binary data back
                        let response = WebSocketMessage::Binary(data);

                        if let Err(e) = ws_server_clone
                            .send_to_client(client_id.clone(), response)
                            .await
                        {
                            tracing::error!("Failed to respond to client {}: {}", client_id, e);
                        }
                    }
                    WebSocketMessage::Ping(_data) => {
                        // Automatically respond with Pong (already handled in the implementation)
                        tracing::info!("Ping received from client {}", client_id);
                    }
                    WebSocketMessage::Close(_) => {
                        tracing::info!("Client {} requested to close connection", client_id);
                    }
                    _ => {
                        tracing::info!(
                            "Received other message type from {}: {:?}",
                            client_id,
                            message
                        );
                    }
                }
            }
        });
    }
}