func Serve(quiet bool) { context, _ := zmq.NewContext() receiver, _ := context.NewSocket(zmq.PULL) receiver.Bind("tcp://*:5562") sender, _ := context.NewSocket(zmq.PUB) sender.Bind("tcp://*:5561") last := time.Now() messages := 0 for { message, err := receiver.Recv(0) if err != nil { fmt.Println(err) } sender.Send(message, 0) if !quiet { messages += 1 now := time.Now() if now.Sub(last).Seconds() > 1 { println(messages, "msg/sec") last = now messages = 0 } } } }
func NewZMQClient(host string) (*ZMQClient, error) { var err error var context *zmq.Context context, err = zmq.NewContext() if err != nil { return nil, err } var pub *zmq.Socket pub, err = context.NewSocket(zmq.PUSH) if err != nil { return nil, err } pub.Connect(fmt.Sprintf("tcp://%s:%d", host, 5562)) var sub *zmq.Socket sub, err = context.NewSocket(zmq.SUB) if err != nil { return nil, err } sub.Connect(fmt.Sprintf("tcp://%s:%d", host, 5561)) return &ZMQClient{context, pub, sub}, nil }