2017-12-02 09:24:26 +01:00
|
|
|
# WebSockets
|
2017-12-14 07:36:28 +01:00
|
|
|
|
2017-12-27 05:52:21 +01:00
|
|
|
Actix supports WebSockets out-of-the-box. It is possible to convert request's `Payload`
|
|
|
|
to a stream of [*ws::Message*](../actix_web/ws/enum.Message.html) with
|
|
|
|
a [*ws::WsStream*](../actix_web/ws/struct.WsStream.html) and then use stream
|
2017-12-27 06:07:51 +01:00
|
|
|
combinators to handle actual messages. But it is simplier to handle websocket communications
|
2017-12-27 05:52:21 +01:00
|
|
|
with http actor.
|
2017-12-14 07:36:28 +01:00
|
|
|
|
2017-12-27 05:52:21 +01:00
|
|
|
```rust
|
|
|
|
extern crate actix;
|
|
|
|
extern crate actix_web;
|
|
|
|
|
|
|
|
use actix::*;
|
|
|
|
use actix_web::*;
|
|
|
|
|
|
|
|
/// Define http actor
|
|
|
|
struct Ws;
|
|
|
|
|
|
|
|
impl Actor for Ws {
|
|
|
|
type Context = HttpContext<Self>;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Define Handler for ws::Message message
|
2017-12-27 06:33:23 +01:00
|
|
|
# impl StreamHandler<ws::Message> for Ws {}
|
|
|
|
impl Handler<ws::Message> for Ws {
|
2017-12-27 05:52:21 +01:00
|
|
|
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>) -> Response<Self, ws::Message>
|
|
|
|
{
|
|
|
|
match msg {
|
|
|
|
ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, &msg),
|
|
|
|
ws::Message::Text(text) => ws::WsWriter::text(ctx, &text),
|
|
|
|
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
Self::empty()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
Application::new()
|
2017-12-27 06:33:23 +01:00
|
|
|
.resource("/ws/", |r| r.f(|req| ws::start(req, Ws))) // <- register websocket route
|
2017-12-27 05:52:21 +01:00
|
|
|
.finish();
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Simple websocket echo server example is available in
|
|
|
|
[examples directory](https://github.com/actix/actix-web/blob/master/examples/websocket.rs).
|
|
|
|
|
|
|
|
Example chat server with ability to chat over websocket connection or tcp connection
|
|
|
|
is available in [websocket-chat directory](https://github.com/actix/actix-web/tree/master/examples/websocket-chat/)
|