英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论