2020-06-19 21:38:11 +02:00
|
|
|
use actix::prelude::*;
|
|
|
|
use actix_web::{middleware, web, App, Error, HttpRequest, HttpResponse, HttpServer};
|
|
|
|
use actix_web_actors::ws;
|
|
|
|
|
|
|
|
async fn ws_index(r: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
|
|
|
|
ws::start(WebSocket::new(), &r, stream)
|
|
|
|
}
|
|
|
|
|
|
|
|
struct WebSocket {}
|
|
|
|
|
|
|
|
impl Actor for WebSocket {
|
|
|
|
type Context = ws::WebsocketContext<Self>;
|
|
|
|
|
|
|
|
fn started(&mut self, _ctx: &mut Self::Context) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for WebSocket {
|
|
|
|
fn handle(
|
|
|
|
&mut self,
|
|
|
|
msg: Result<ws::Message, ws::ProtocolError>,
|
|
|
|
ctx: &mut Self::Context,
|
|
|
|
) {
|
|
|
|
if let Ok(msg) = msg {
|
|
|
|
match msg {
|
|
|
|
ws::Message::Text(text) => ctx.text(text),
|
|
|
|
ws::Message::Binary(bin) => ctx.binary(bin),
|
|
|
|
ws::Message::Ping(bytes) => ctx.pong(&bytes),
|
2020-06-25 06:20:27 +02:00
|
|
|
ws::Message::Close(reason) => {
|
|
|
|
ctx.close(reason);
|
|
|
|
ctx.stop();
|
|
|
|
}
|
2020-06-19 21:38:11 +02:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ctx.stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WebSocket {
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
|
|
|
std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
HttpServer::new(|| {
|
|
|
|
App::new()
|
|
|
|
.wrap(middleware::Logger::default())
|
|
|
|
.service(web::resource("/").route(web::get().to(ws_index)))
|
|
|
|
})
|
|
|
|
.bind("127.0.0.1:9001")?
|
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|