Esempio n. 1
0
func main() {
	context := zmq.Context()
	defer context.Close()

	//  Connect our subscriber socket
	subscriber := context.Socket(zmq.SUB)
	defer subscriber.Close()
	subscriber.SetSockOptString(zmq.IDENTITY, "Hello")
	subscriber.SetSockOptString(zmq.SUBSCRIBE, "")
	subscriber.Connect("tcp://localhost:5565")

	//  Synchronize with publisher
	sync := context.Socket(zmq.PUSH)
	defer sync.Close()
	sync.Connect("tcp://localhost:5564")
	sync.Send([]byte{}, 0)

	//  Get updates, expect random Ctrl-C death
	for {
		data, _ := subscriber.Recv(0)
		str := string(data)
		println(str)
		if str == "END" {
			break
		}
	}
}
Esempio n. 2
0
func main() {
	context := zmq.Context()
	defer context.Close()

	//  Subscriber tells us when it's ready here
	sync := context.Socket(zmq.PULL)
	defer sync.Close()
	sync.Bind("tcp://*:5564")

	//  We send updates via this socket
	publisher := context.Socket(zmq.PUB)
	defer publisher.Close()
	publisher.Bind("tcp://*:5565")

	//  Wait for synchronization request
	sync.Recv(0)

	for update_nbr := 0; update_nbr < 10; update_nbr++ {
		str := fmt.Sprintf("Update %d", update_nbr)
		publisher.Send([]byte(str), 0)
		time.Sleep(1e9)
	}
	publisher.Send([]byte("END"), 0)

	time.Sleep(1)
}