use actix::{Actor, StreamHandler}; use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer}; use actix_web_actors::ws; /// Define HTTP actor struct MyWs; impl Actor for MyWs { type Context = ws::WebsocketContext; } /// Handler for ws::Message message impl StreamHandler> for MyWs { fn handle(&mut self, msg: Result, ctx: &mut Self::Context) { match msg { Ok(ws::Message::Ping(msg)) => ctx.pong(&msg), Ok(ws::Message::Text(text)) => ctx.text(text), Ok(ws::Message::Binary(bin)) => ctx.binary(bin), _ => (), } } } async fn index(req: HttpRequest, stream: web::Payload) -> Result { let resp = ws::start(MyWs {}, &req, stream); println!("{:?}", resp); resp } #[actix_web::main] async fn main() -> std::io::Result<()> { HttpServer::new(|| App::new().route("/ws/", web::get().to(index))) .bind("127.0.0.1:8080")? .run() .await }