dongxian7489 2017-04-20 06:50
浏览 290
已采纳

是否可以将ZMQ代理用作“交换机”?

I'm going to implement the following diagram: ZMQ-proxy

Just to let all "data generators" publish data to all "data receivers". Data receivers should still have ability to set subscritions.

// Proxy
package main

import zmq "github.com/pebbe/zmq4"

func main() {
    publisher, _ := zmq.NewSocket(zmq.XPUB)
    defer publisher.Close()
    publisher.Bind("tcp://*:8080")

    subscriber, _ := zmq.NewSocket(zmq.XSUB)
    defer subscriber.Close()
    subscriber.Bind("tcp://*:8081")

    zmq.Proxy(subscriber, publisher, nil)
}

And "data generators":

// Data generator
package main

import (
    zmq "github.com/pebbe/zmq4"
    "time"
)

func main() {
    publisher, _ := zmq.NewSocket(zmq.PUB)
    defer publisher.Close()
    publisher.Connect("tcp://127.0.0.1:8081")

    for {
        publisher.Send("Some msg", 0)
        time.Sleep(time.Second)
    }
}

And "data receivers":

// Data receiver
package main

import (
    "fmt"
    zmq "github.com/pebbe/zmq4"
)

func main() {
    subscriber, _ := zmq.NewSocket(zmq.SUB)
    defer subscriber.Close()
    subscriber.Connect("tcp://127.0.0.1:8080")

    for {
        msg, _ := subscriber.Recv(0)
        fmt.Println(msg)
    }
}

It seems to me that it should work, but it isn't. What am I doing wrong?

展开全部

  • 写回答

1条回答 默认 最新

  • dtzjvj3915 2017-04-20 09:12
    关注

    To receive any data subscriber socket should specify SetSubscibe with some filter. see Official documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 距离软磁铁一定距离的磁感应强度大小怎么求
  • ¥15 霍尔传感器hmc5883l的xyz轴输出和该点的磁感应强度大小的关系是什么
  • ¥15 vscode开发micropython,import模块出现异常
  • ¥20 Excel数据自动录入表单并提交
  • ¥30 silcavo仿真,30分钟,只需要代码
  • ¥15 FastReport 怎么实现打印后马上关闭打印预览窗口
  • ¥15 利用3支股票数据估计其均值和方差的95%置信区间。
  • ¥15 微信小程序运行一项功能时,弹出未知错误弹框,检查代码没有问题
  • ¥15 ATAC测序生成self-pseudo replicates之前是否要进行去线粒体reads
  • ¥15 python模糊字匹配函数问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部