SockJS server for rust language
Clone or download
Latest commit 5206ea6 May 18, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
examples fix examples Apr 19, 2018
protocol use protcol tests for coverage Oct 31, 2017
src use actix web 0.6 May 17, 2018
tests add readme example and skeptic Oct 30, 2017
.cov.sh use protcol tests for coverage Oct 31, 2017
.gitattributes github attrs Oct 31, 2017
.gitignore repo prep work Oct 9, 2017
.travis.yml do not run skeptic Mar 3, 2018
CHANGES.md use actix web 0.6 May 17, 2018
Cargo.toml use actix web 0.6 May 17, 2018
LICENSE repo prep work Oct 9, 2017
Makefile add readme example and skeptic Oct 30, 2017
README.md fix readme example, bump rustc version Mar 3, 2018
build.rs add readme example and skeptic Oct 30, 2017

README.md

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:

[dependencies]
sockjs = "0.2"

Supported transports

Simple chat example

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<Self>;
}

/// 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<Self>) {
        ctx.broadcast("Someone joined.")
    }
    fn closed(&mut self, ctx: &mut SockJSContext<Self>, _: CloseReason) {
        ctx.broadcast("Someone left.")
    }
}

/// Session has to be able to handle `sockjs::Message` messages
impl Handler<Message> for Chat {
    type Result = ();

    fn handle(&mut self, msg: Message, ctx: &mut SockJSContext<Self>)
    {
        // broadcast message to all sessions
        ctx.broadcast(msg);
    }
}


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

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

    HttpServer::new(move || {
        let manager = sm.clone();
        Application::new()
            // register SockJS application
            .handler(
                "/sockjs/", sockjs::SockJS::new(manager.clone()))})
        .bind("127.0.0.1:8080").unwrap()
        .start();

    // let _ = sys.run();
}

Full chat example