0
我要實現如下圖: ZMQ-proxy是否可以使用ZMQ代理作爲「開關」?
只是爲了讓所有的「數據發生器」數據發佈到所有的「數據接收器」。數據接收器應該仍然有能力設置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)
}
和 「數據發生器」:
// 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)
}
}
和 「數據接收器」:
// 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)
}
}
在我看來,它應該工作,但事實並非如此。我究竟做錯了什麼?
在Proxy的套接字監視器中,我看到了EVENT_ACCEPTED和EVENT_DISCONNECTED消息。 –