forked from booksbyus/zguide
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Aleksandar Janicijevic
committed
Feb 25, 2013
1 parent
65f1843
commit c6f5e8b
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Synchronized subscriber | ||
// | ||
// Author: Aleksandar Janicijevic | ||
// Requires: http://github.com/alecthomas/gozmq | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
zmq "github.com/alecthomas/gozmq" | ||
"time" | ||
) | ||
|
||
func main() { | ||
context, _ := zmq.NewContext() | ||
defer context.Close() | ||
|
||
subscriber, _ := context.NewSocket(zmq.SUB) | ||
defer subscriber.Close() | ||
subscriber.Connect("tcp://localhost:5561") | ||
subscriber.SetSockOptString(zmq.SUBSCRIBE, "") | ||
|
||
// 0MQ is so fast, we need to wait a while... | ||
time.Sleep(time.Second) | ||
|
||
// Second, synchronize with publisher | ||
syncclient, _ := context.NewSocket(zmq.REQ) | ||
defer syncclient.Close() | ||
syncclient.Connect("tcp://localhost:5562") | ||
|
||
// - send a synchronization request | ||
fmt.Println("Send synchronization request") | ||
syncclient.Send([]byte(""), 0) | ||
fmt.Println("Wait for synchronization reply") | ||
|
||
// - wait for synchronization reply | ||
syncclient.Recv(0) | ||
|
||
fmt.Println("Get updates") | ||
// Third, get our updates and report how many we got | ||
update_nbr := 0 | ||
for { | ||
reply, _ := subscriber.Recv(0) | ||
if string(reply) == "END" { | ||
break | ||
} | ||
update_nbr++ | ||
} | ||
fmt.Printf("Received %d updates\n", update_nbr) | ||
} |