SockJS server Build Status codecov crates.io

SockJS server for for Actix framework.


Actix SockJS is licensed under the Apache-2.0 license.

Usage

To use sockjs, add this to your Cargo.toml:

toml [dependencies] sockjs = "0.1"

Supported transports

Simple chat example

```rust extern crate actix; extern crate actix_web; extern crate sockjs;

use actix_web::; use actix::prelude::; use sockjs::{Message, Session, CloseReason, SockJSManager, SockJSContext};

struct Chat;

/// SockJSContext context is required for sockjs session impl Actor for Chat { type Context = SockJSContext; }

/// Session has to implement Default trait impl Default for Chat { fn default() -> Chat { Chat } }

/// Sockjs session trait impl Session for Chat { fn opened(&mut self, ctx: &mut SockJSContext) { ctx.broadcast("Someone joined.") } fn closed(&mut self, ctx: &mut SockJSContext, _: CloseReason) { ctx.broadcast("Someone left.") } }

/// Session has to be able to handle sockjs::Message messages impl Handler for Chat { fn handle(&mut self, msg: Message, ctx: &mut SockJSContext) -> Response { // broadcast message to all sessions ctx.broadcast(msg); Self::empty() } }

fn main() { let sys = actix::System::new("sockjs-chat");

// SockJS sessions manager
let sm: SyncAddress<_> = SockJSManager::<Chat>::start_default();

HttpServer::new(
    Application::default("/")
        // register SockJS application
        .route_handler(
            "/sockjs/", sockjs::SockJS::<Chat, _>::new(sm.clone())))
    .serve::<_, ()>("127.0.0.1:8080").unwrap();

Arbiter::system().send(msgs::SystemExit(0));
let _ = sys.run();

} ```

Full chat example