#channel #events #phoenix-channels

bin+lib phoenix

Rust的Phoenix通道客户端

2个版本

使用旧的Rust 2015

0.1.2 2018年3月17日
0.1.1 2018年3月17日

WebSocket中排名第254

MIT许可证

14KB
301

凤凰通道客户端(Rust语言编写)

这个库试图在Rust中创建Phoenix客户端。

此Crate与https://github.com/laibulle/phoenix_channel_demo进行了测试

示例

git clone [email protected]:laibulle/phoenix_channel_demo.git
cd phoenix_channel_demo
mix deps.get
mix phx.server
extern crate phoenix;
#[macro_use]
extern crate serde_json;

extern crate env_logger;

use std::{thread, time};
use phoenix::{Phoenix, Event};

fn main() {
    env_logger::init();
    
    let url = "ws://127.0.0.1:4000/socket";

    // Simulate a user
    thread::spawn(move || {
        let mut phx = Phoenix::new(url);
        let mutex_chan = phx.channel("room:lobby").clone();

        {
            let mut device_chan = mutex_chan.lock().unwrap();
            device_chan.join();
        }

        loop {
            match phx.out.recv() {
                Ok(msg) => println!("user1: {:?}", msg),
                Err(_err) => ()//println!("{:?}", err)
            }
        }
    });

    thread::sleep(time::Duration::from_millis(500));

    // Simulate an other user
    let mut phx = Phoenix::new(url);
    let mutex_chan = phx.channel("room:lobby").clone();

    {
        let mut device_chan = mutex_chan.lock().unwrap();
        device_chan.join();
        device_chan.send(Event::Custom("new_msg".to_string()), serde_json::from_str(r#"{"body": "Hello"}"#).unwrap());
    }

    loop {
        match phx.out.recv() {
            Ok(msg) => println!("user2: {:?}", msg),
            Err(_err) => ()//println!("{:?}", err)
        }
    }
}

RUST_LOG=debug cargo run

依赖关系

~7–17MB
~245K SLoC