本文整理匯總了Golang中github.com/pebbe/zmq4.Socket.SetSubscribe方法的典型用法代碼示例。如果您正苦於以下問題:Golang Socket.SetSubscribe方法的具體用法?Golang Socket.SetSubscribe怎麽用?Golang Socket.SetSubscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/pebbe/zmq4.Socket
的用法示例。
在下文中一共展示了Socket.SetSubscribe方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createEventSubSocket
func createEventSubSocket() (*zmq4.Socket, error) {
var err error
var subSocket *zmq4.Socket
if subSocket, err = zmq4.NewSocket(zmq4.SUB); err != nil {
return nil, fmt.Errorf("NewSocket %s", err)
}
if err = subSocket.SetSubscribe("web-writer-start"); err != nil {
return nil, fmt.Errorf("SetSubscribe %s", err)
}
return subSocket, nil
}
示例2: Add
// Add adds a new sink channel for messages of topic `topic`
func (r *Router) Add(topic string, sink chan<- []byte) error {
var err error
var socket *zmq.Socket
socket, err = r.ctx.NewSocket(zmq.SUB)
if err != nil {
return err
}
if err = socket.Connect(r.addr); err != nil {
return err
}
if err = socket.SetSubscribe(topic); err != nil {
return err
}
r.poller.Add(socket, zmq.POLLIN)
r.receivers[socket] = &receiver{sink: sink}
return nil
}
示例3: TestDisconnectInproc
func TestDisconnectInproc(t *testing.T) {
var pubSocket, subSocket *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{pubSocket, subSocket} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
publicationsReceived := 0
isSubscribed := false
pubSocket, err := zmq.NewSocket(zmq.XPUB)
if err != nil {
t.Fatal("NewSocket XPUB:", err)
}
subSocket, err = zmq.NewSocket(zmq.SUB)
if err != nil {
t.Fatal("NewSocket SUB:", err)
}
err = subSocket.SetSubscribe("foo")
if err != nil {
t.Fatal("subSocket.SetSubscribe:", err)
}
err = pubSocket.Bind("inproc://someInProcDescriptor")
if err != nil {
t.Fatal("pubSocket.Bind:", err)
}
iteration := 0
poller := zmq.NewPoller()
poller.Add(subSocket, zmq.POLLIN) // read publications
poller.Add(pubSocket, zmq.POLLIN) // read subscriptions
for {
sockets, err := poller.Poll(100 * time.Millisecond)
if err != nil {
t.Error("Poll:", err)
break // Interrupted
}
for _, socket := range sockets {
if socket.Socket == pubSocket {
for {
buffer, err := pubSocket.Recv(0)
if err != nil {
t.Fatal("pubSocket.Recv", err)
}
exp := "\x01foo"
if isSubscribed {
exp = "\x00foo"
}
if buffer != exp {
t.Errorf("pubSocket.Recv: expected %q, got %q", exp, buffer)
}
if buffer[0] == 0 {
if isSubscribed != true {
t.Errorf("Poller: expected subscribed")
}
isSubscribed = false
} else {
if isSubscribed != false {
t.Errorf("Poller: expected not subscribed")
}
isSubscribed = true
}
more, err := pubSocket.GetRcvmore()
if err != nil {
t.Fatal("pubSocket.GetRcvmore:", err)
}
if !more {
break // Last message part
}
}
break
}
}
for _, socket := range sockets {
if socket.Socket == subSocket {
for _, exp := range []string{"foo", "this is foo!", "", ""} {
msg, err := subSocket.Recv(0)
if err != nil {
t.Fatal("subSocket.Recv:", err)
}
if msg != exp {
t.Errorf("subSocket.Recv: expected %q, got %q", exp, msg)
}
more, err := subSocket.GetRcvmore()
if err != nil {
t.Fatal("subSocket.GetRcvmore:", err)
}
if !more {
publicationsReceived++
//.........這裏部分代碼省略.........
示例4: main
func main() {
flag.Parse(nil)
address_list := flag.Args
if len(address_list) == 0 {
fmt.Println("No Addresses submitted")
fmt.Println(flag.Help())
return
}
var send, recv bool
skip := false
var socket *zmq.Socket
switch *socket_type {
case "PUSH":
socket, _ = zmq.NewSocket(zmq.PUSH)
send = true
case "PULL":
socket, _ = zmq.NewSocket(zmq.PULL)
recv = true
case "PUB":
socket, _ = zmq.NewSocket(zmq.PUB)
send = true
case "SUB":
socket, _ = zmq.NewSocket(zmq.SUB)
recv = true
if len(*subscriptions) == 0 {
socket.SetSubscribe("")
}
for _, subscription := range *subscriptions {
socket.SetSubscribe(subscription)
}
case "REQ":
socket, _ = zmq.NewSocket(zmq.REQ)
send = true
recv = true
case "REP":
socket, _ = zmq.NewSocket(zmq.REP)
send = true
recv = true
skip = true
}
defer socket.Close()
// connect or bind
if *mode {
for _, address := range address_list {
socket.Connect(address)
}
} else {
for _, address := range address_list {
socket.Bind(address)
}
}
delim := byte('\n')
if *null {
fmt.Println("Setting delim to null")
delim = byte(0x00)
}
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
for i := 0; i < *number || *number == -1; i++ {
if send && !skip {
line, _ := reader.ReadBytes(delim)
socket.SendBytes([]byte(line), 0)
}
if recv {
data, _ := socket.RecvBytes(0)
writer.Write(data)
writer.Flush()
}
if skip {
skip = false
}
}
fmt.Println("finished", *number)
}