Module actix_web::ws [] [src]

WebSocket support for Actix

To setup a WebSocket, first do web socket handshake then on success convert Payload into a WsStream stream and then use WsWriter to communicate with the peer.

Example

use actix_web::ws;

// do websocket handshake and start actor
fn ws_index(req: HttpRequest) -> Result<HttpResponse> {
    ws::start(req, Ws)
}

struct Ws;

impl Actor for Ws {
    type Context = ws::WebsocketContext<Self>;
}

// Handler for ws::Message messages
impl StreamHandler<ws::Message, ws::WsError> for Ws {

    fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
        match msg {
            ws::Message::Ping(msg) => ctx.pong(&msg),
            ws::Message::Text(text) => ctx.text(text),
            ws::Message::Binary(bin) => ctx.binary(bin),
            _ => (),
        }
    }
}

Structs

WebsocketContext

WebSockets actor execution context

WsClient

WebSocket client

WsClientHandshake
WsClientReader
WsClientWriter
WsStream

Maps Payload stream into stream of ws::Message items

Enums

CloseCode

Status code used to indicate why an endpoint is closing the WebSocket connection.

Message

WebSocket Message

WsClientError

Websocket client error

WsError

Websocket errors

WsHandshakeError

Websocket handshake errors

Functions

handshake

Prepare WebSocket handshake response.

start

Do websocket handshake and start actor