1
0
mirror of https://github.com/actix/actix-website synced 2025-02-08 22:36:07 +01:00

Update websockets

This commit is contained in:
Yuki Okushi 2019-12-29 04:16:54 +09:00
parent 7a973d6fe9
commit 9f385d384b
3 changed files with 20 additions and 15 deletions

View File

@ -18,7 +18,7 @@ The following is an example of a simple websocket echo server:
> An example chat server with the ability to chat over a websocket or tcp connection > An example chat server with the ability to chat over a websocket or tcp connection
> is available in [websocket-chat directory][chat] > is available in [websocket-chat directory][chat]
[message]: https://docs.rs/actix-web-actors/1.0.0/actix_web_actors/ws/enum.Message.html [message]: https://docs.rs/actix-web-actors/2/actix_web_actors/ws/enum.Message.html
[payload]: https://docs.rs/actix-web/1.0.2/actix_web/web/struct.Payload.html [payload]: https://docs.rs/actix-web/2/actix_web/web/struct.Payload.html
[examples]: https://github.com/actix/examples/tree/master/websocket/ [examples]: https://github.com/actix/examples/tree/master/websocket/
[chat]: https://github.com/actix/examples/tree/master/websocket-chat/ [chat]: https://github.com/actix/examples/tree/master/websocket-chat/

View File

@ -4,6 +4,7 @@ version = "1.0.0"
edition = "2018" edition = "2018"
[dependencies] [dependencies]
actix = "0.8" actix = "0.9"
actix-web = "1.0" actix-rt = "1.0"
actix-web-actors = "1.0" actix-web = "2.0"
actix-web-actors = "2.0"

View File

@ -11,29 +11,33 @@ impl Actor for MyWs {
} }
/// Handler for ws::Message message /// Handler for ws::Message message
impl StreamHandler<ws::Message, ws::ProtocolError> for MyWs { impl StreamHandler<Result<ws::Message, ws::ProtocolError>> for MyWs {
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { fn handle(
&mut self,
msg: Result<ws::Message, ws::ProtocolError>,
ctx: &mut Self::Context,
) {
match msg { match msg {
ws::Message::Ping(msg) => ctx.pong(&msg), Ok(ws::Message::Ping(msg)) => ctx.pong(&msg),
ws::Message::Text(text) => ctx.text(text), Ok(ws::Message::Text(text)) => ctx.text(text),
ws::Message::Binary(bin) => ctx.binary(bin), Ok(ws::Message::Binary(bin)) => ctx.binary(bin),
_ => (), _ => (),
} }
} }
} }
fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> { async fn index(req: HttpRequest, stream: web::Payload) -> Result<HttpResponse, Error> {
let resp = ws::start(MyWs {}, &req, stream); let resp = ws::start(MyWs {}, &req, stream);
println!("{:?}", resp); println!("{:?}", resp);
resp resp
} }
fn main() { #[actix_rt::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/ws/", web::get().to(index))) HttpServer::new(|| App::new().route("/ws/", web::get().to(index)))
.bind("127.0.0.1:8088") .bind("127.0.0.1:8088")?
.unwrap()
.run() .run()
.unwrap(); .await
} }
// </websockets> // </websockets>