use async_trait::async_trait; use drama::{Actor, ActorHandle, Handler, Relay, RelayActor}; use flume::Sender; use futures::stream::SplitStream; use futures::StreamExt; use parking_lot::RwLock; use std::collections::HashMap; use std::sync::atomic::AtomicUsize; use std::sync::Arc; use warp::ws::{Message, WebSocket}; use warp::Filter; struct WebsocketActor { hello: ActorHandle, tx: Sender, } impl WebsocketActor { fn new(handle: ActorHandle, tx: Sender) -> Self { Self { hello: handle, tx } } } impl Actor for WebsocketActor {} impl RelayActor> for WebsocketActor { type Error = warp::Error; } #[async_trait] impl Relay for WebsocketActor { async fn process(&mut self, message: Message) -> Option { self.hello .send(crate::Msg { _content: message.to_str().unwrap().to_owned(), }) .unwrap_or_else(|e| tracing::trace!("FUKEN HELL M8 {e}")); self.tx.send(message.clone()).unwrap(); Some(message) } } struct Hello {} impl Actor for Hello {} #[derive(Clone)] struct Msg { pub _content: String, } #[async_trait] impl Handler for Hello { type Response = usize; async fn handle(&mut self, _: &Msg) -> usize { 10 } } type Arbiter = Arc>>>; static ID: AtomicUsize = AtomicUsize::new(0); #[tokio::main] async fn main() { let pool = Arc::new(RwLock::new(HashMap::new())); let pool = warp::any().map(move || pool.clone()); let hello = Hello {}.start(); let hello = warp::any().map(move || hello.clone()); // GET /chat -> websocket upgrade let chat = warp::path("chat") // The `ws()` filter will prepare Websocket handshake... .and(warp::ws()) .and(pool) .and(hello) .map( |ws: warp::ws::Ws, pool: Arbiter, hello: ActorHandle| { // This will call our function if the handshake succeeds. ws.on_upgrade(|socket| async move { let (si, st) = socket.split(); let (tx, rx) = flume::unbounded(); let actor = WebsocketActor::new(hello, tx.clone()); let handle = actor.start_relay(st, tx); tokio::spawn(rx.into_stream().map(Ok).forward(si)); let id = ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed); tracing::trace!("Adding actor {id}"); pool.write().insert(id, handle); }) }, ); // GET / -> index html let index = warp::path::end().map(|| warp::reply::html(INDEX_HTML)); let routes = index.or(chat); warp::serve(routes).run(([127, 0, 0, 1], 3030)).await } static INDEX_HTML: &str = r#" Warp Chat

Warp chat

Connecting...

"#;