可以将ZMQ代理用作“开关”吗?

huangapple go评论71阅读模式
英文:

Is it possible to use ZMQ proxy as "switch"?

问题

我将实现以下图表:
ZMQ代理

只需让所有的“数据生成器”将数据发布给所有的“数据接收器”。
数据接收器仍然应该能够设置订阅。

// 代理
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)
}

和“数据生成器”:

// 数据生成器
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)
    }
}

和“数据接收器”:

// 数据接收器
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)
    }
}

在我看来,它应该可以工作,但实际上并没有。我做错了什么?

英文:

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

得分: 1

要接收任何数据,订阅者套接字应该使用某个过滤器来指定SetSubscribe。请参阅官方文档

英文:

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

huangapple
  • 本文由 发表于 2017年4月20日 22:50:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/43522919.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定