kegani 0.1.0

A developer-friendly, ergonomic, production-ready Rust web framework
Documentation
//! WebSocket handler for Kegani
//!
//! Provides WebSocket support for real-time communication.
//!
//! Note: This is a simplified implementation. For full WebSocket support,
//! enable the `websocket` feature in Cargo.toml.

use actix_web::{web, Error, HttpRequest, HttpResponse};

/// WebSocket handler configuration
#[derive(Clone)]
pub struct WebSocketConfig {
    /// Ping interval in seconds
    pub ping_interval: u64,
    /// Ping timeout in seconds
    pub ping_timeout: u64,
    /// Buffer size
    pub buffer_size: usize,
}

impl Default for WebSocketConfig {
    fn default() -> Self {
        Self {
            ping_interval: 30,
            ping_timeout: 10,
            buffer_size: 65536,
        }
    }
}

/// WebSocket handler
pub struct WebSocketHandler {
    config: WebSocketConfig,
}

impl WebSocketHandler {
    /// Create a new WebSocket handler
    pub fn new() -> Self {
        Self {
            config: WebSocketConfig::default(),
        }
    }

    /// Set ping interval
    pub fn ping_interval(mut self, interval: u64) -> Self {
        self.config.ping_interval = interval;
        self
    }

    /// Set buffer size
    pub fn buffer_size(mut self, size: usize) -> Self {
        self.config.buffer_size = size;
        self
    }

    /// Handle WebSocket connection
    pub async fn handle(
        req: HttpRequest,
        stream: web::Payload,
    ) -> Result<HttpResponse, Error> {
        // WebSocket support requires the `websocket` feature
        // For now, return a simple response indicating WebSocket upgrade is not available
        Ok(HttpResponse::NotImplemented()
            .content_type("text/plain")
            .body("WebSocket support requires the `websocket` feature. Add `websocket = [\"actix-web-actors\"]` to your Cargo.toml features."))
    }
}

impl Default for WebSocketHandler {
    fn default() -> Self {
        Self::new()
    }
}