Pure zmq4 based on golang 오픈소스를 활용하여 PUB/SUB을 테스트
- Publish golang 소스코드 작성
- Subscription golang 소스코드 작성
- 테스트
ZeroMq 오픈소스 URL
go-zeromq/zmq4
[WIP] Pure-Go implementation of ZeroMQ-4. Contribute to go-zeromq/zmq4 development by creating an account on GitHub.
github.com
download go command
- go get github.com/go-zeromq/zmq4
package main
import (
"context"
"log"
"time"
"github.com/go-zeromq/zmq4"
)
func main() {
log.SetPrefix("psenvpub: ")
// prepare the publisher
pub := zmq4.NewPub(context.Background())
defer pub.Close()
err := pub.Listen("tcp://*:5563")
if err != nil {
log.Fatalf("could not listen: %v", err)
}
msgA := zmq4.NewMsgFrom(
[]byte("test1"), //This message has named "test1" key
[]byte("My key is test1"),
)
msgB := zmq4.NewMsgFrom(
[]byte("test2"), //This message has named "test2" key
[]byte("my key is test2"),
)
for {
err = pub.Send(msgA)
if err != nil {
log.Fatal(err)
}
err = pub.Send(msgB)
if err != nil {
log.Fatal(err)
}
time.Sleep(time.Second)
}
}
publish 테스트코드
package main
import (
"context"
"log"
"github.com/go-zeromq/zmq4"
)
func main() {
log.SetPrefix("psenvsub: ")
// Prepare our subscriber
sub := zmq4.NewSub(context.Background())
defer sub.Close()
err := sub.Dial("tcp://localhost:5563")
if err != nil {
log.Fatalf("could not dial: %v", err)
}
err = sub.SetOption(zmq4.OptionSubscribe, "test1") //Received only named "test1" key.
if err != nil {
log.Fatalf("could not subscribe: %v", err)
}
for {
// Read envelope
msg, err := sub.Recv()
if err != nil {
log.Fatalf("could not receive message: %v", err)
}
log.Printf("[%s] %s\n", msg.Frames[0], msg.Frames[1])
}
}
Subscription 테스트코드
'언어 > Golang' 카테고리의 다른 글
Go 오픈소스를 이용하여 C코드에서 활용 할 수 있는 Library 테스트 (0) | 2020.11.23 |
---|---|
Golang (0) | 2017.05.22 |