1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//! WebSocket support for Tideway
//!
//! This module provides WebSocket functionality for real-time communication
//! in Tideway applications. It includes connection management, room/channel
//! support, and broadcasting capabilities.
//!
//! # Example
//!
//! ```rust,no_run
//! use tideway::websocket::{ws, ConnectionManager, WebSocketHandler, Connection, Message};
//! use tideway::{App, AppContext, Result};
//! use async_trait::async_trait;
//! use std::sync::Arc;
//!
//! struct ChatHandler;
//!
//! #[async_trait]
//! impl WebSocketHandler for ChatHandler {
//! async fn on_connect(&self, conn: &mut Connection, _ctx: &AppContext) -> Result<()> {
//! conn.send_text("Welcome!").await?;
//! Ok(())
//! }
//!
//! async fn on_message(&self, conn: &mut Connection, msg: Message, ctx: &AppContext) -> Result<()> {
//! if let Message::Text(text) = msg {
//! let manager = ctx.websocket_manager()?;
//! manager.broadcast_text(format!("{}: {}", conn.id(), text)).await?;
//! }
//! Ok(())
//! }
//!
//! async fn on_disconnect(&self, _conn: &mut Connection, _ctx: &AppContext) {
//! // Cleanup
//! }
//! }
//!
//! #[tokio::main]
//! async fn main() {
//! let manager = Arc::new(ConnectionManager::new());
//! let ctx = AppContext::builder()
//! .with_websocket_manager(manager.clone())
//! .build();
//!
//! let app = App::new()
//! .with_context(ctx.clone())
//! .merge_router(ws("/ws", ChatHandler, manager).with_state(ctx));
//!
//! app.serve().await.unwrap();
//! }
//! ```
pub use Connection;
pub use ws;
pub use ;
pub use ;
pub use Room;
pub use WebSocketHandler;