3

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

我在单独的控制台窗口中执行了这些操作:

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

    表明

    Subscribed to '[104, 111, 103, 101]'.
    Device is ready.
    
  2. cargo run --example pubsub -- client hoge

    表明

    Subscribed to '[104, 111, 103, 101]'.
    
  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'.
    ...
    

所有三个命令都在运行,没有一个退出。我希望控制台 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'.
...

但是什么都没有显示。

我的环境是

  • Max OS X 塞拉利昂
  • nanomsg 1.0.0
  • 锈病 1.16.0
4

1 回答 1

5

这是服务器代码的问题,已在 master 分支 ( #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
    }
}

发布的消息是使用format!宏构建的,该宏不正确地将主题打印为字节数组,而不是文本片段。不同的主题标识符导致没有订阅者收到消息。

该示例已由当前维护者在此处修复。作为结束说明,此 API 的用户必须记住,发布消息的第一个字节始终引用订阅主题。

于 2017-03-20T21:50:38.140 回答