use std::net::SocketAddr; use async_trait::async_trait; use tokio::net::TcpListener; type SessionID = u16; type Session = ezsockets::Session; struct EchoServer {} #[async_trait] impl ezsockets::ServerExt for EchoServer { type Session = EchoSession; type Call = (); async fn on_connect( &mut self, socket: ezsockets::Socket, _request: ezsockets::Request, address: SocketAddr, ) -> Result> { let id = address.port(); let session = Session::create(|handle| EchoSession { id, handle }, id, socket); Ok(session) } async fn on_disconnect( &mut self, _id: ::ID, _reason: Result, ezsockets::Error>, ) -> Result<(), ezsockets::Error> { Ok(()) } async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> { let () = call; Ok(()) } } struct EchoSession { handle: Session, id: SessionID, } #[async_trait] impl ezsockets::SessionExt for EchoSession { type ID = SessionID; type Call = (); fn id(&self) -> &Self::ID { &self.id } async fn on_text(&mut self, text: String) -> Result<(), ezsockets::Error> { self.handle.text(text).unwrap(); Ok(()) } async fn on_binary(&mut self, _bytes: Vec) -> Result<(), ezsockets::Error> { unimplemented!() } async fn on_call(&mut self, call: Self::Call) -> Result<(), ezsockets::Error> { let () = call; Ok(()) } } pub async fn run(listener: std::net::TcpListener) { let listener = TcpListener::from_std(listener).unwrap(); let (server, _) = ezsockets::Server::create(|_| EchoServer {}); ezsockets::tungstenite::run_on(server, listener, ezsockets::tungstenite::Acceptor::Plain) .await .unwrap(); }