nanomsg板条箱的示例不起作用

Ats*_*shi 3 rust nanomsg

我尝试了Rust nanomsg pubsub示例,但它不起作用.

我在单独的控制台窗口中执行了以下每个操作:

  1. cargo run --example pubsub -- device hoge

    表明

    Subscribed to '[104, 111, 103, 101]'.
    Device is ready.
    
    Run Code Online (Sandbox Code Playgroud)
  2. cargo run --example pubsub -- client hoge

    表明

    Subscribed to '[104, 111, 103, 101]'.
    
    Run Code Online (Sandbox Code Playgroud)
  3. cargo run --example pubsub -- server hoge

    表明

    Server is ready.
    Published '[104, 111, 103, 101] #1'.
    Published '[104, 111, 103, 101] #2'.
    Published '[104, 111, 103, 101] #3'.
    ...
    
    Run Code Online (Sandbox Code Playgroud)

所有三个命令都继续运行,没有一个退出.我预计控制台2会显示:

Subscribed to '[104, 111, 103, 101]'.
Recv '[104, 111, 103, 101] #1'.
Recv '[104, 111, 103, 101] #2'.
Recv '[104, 111, 103, 101] #3'.
...
Run Code Online (Sandbox Code Playgroud)

但没有显示任何内容.

我的环境是

  • Max OS X Sierra
  • nanomsg 1.0.0
  • rustc 1.16.0

E_n*_*ate 5

这是服务器代码的问题,已在主分支中修复(#173).这是错误的片段(来自repo):

let msg = format!("{:?} #{}", topic,  count);
match socket.write_all(msg.as_bytes()) {
    Ok(..) => println!("Published '{}'.", msg),
    Err(err) => {
        println!("Server failed to publish '{}'.", err);
        break
    }
}
Run Code Online (Sandbox Code Playgroud)

发布的消息是使用format!宏构建的,它不恰当地将主题打印为一个字节数组,而不是一段文本.不同的主题标识符导致没有订阅者接收该消息.

这个例子是固定在这里由当前的维护者.作为结束注释,此API的用户必须记住,发布消息的第一个字节始终引用订阅主题.