本文整理汇总了Golang中github.com/pebbe/zmq3.Socket.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang Socket.Close方法的具体用法?Golang Socket.Close怎么用?Golang Socket.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pebbe/zmq3.Socket
的用法示例。
在下文中一共展示了Socket.Close方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSocketEvent
func TestSocketEvent(t *testing.T) {
var rep *zmq.Socket
defer func() {
if rep != nil {
rep.SetLinger(0)
rep.Close()
}
}()
// REP socket
rep, err := zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
// REP socket monitor, all events
err = rep.Monitor("inproc://monitor.rep", zmq.EVENT_ALL)
if err != nil {
t.Fatal("rep.Monitor:", err)
}
chMsg := make(chan string, 10)
go rep_socket_monitor("inproc://monitor.rep", chMsg)
time.Sleep(time.Second)
// Generate an event
err = rep.Bind("tcp://*:9689")
if err != nil {
t.Fatal("rep.Bind:", err)
}
rep.Close()
rep = nil
expect := []string{
"EVENT_LISTENING tcp://0.0.0.0:9689",
"EVENT_CLOSED tcp://0.0.0.0:9689",
"Done",
}
i := 0
for msg := range chMsg {
if i < len(expect) {
if msg != expect[i] {
t.Errorf("Expected message %q, got %q", expect[i], msg)
}
i++
} else {
t.Error("Unexpected message: %q", msg)
}
}
for ; i < len(expect); i++ {
t.Errorf("Expected message %q, got nothing", expect[i])
}
}
示例2: TestPoller
func TestPoller(t *testing.T) {
var sb, sc *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{sb, sc} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
sb, err := zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sb.Bind("tcp://127.0.0.1:9737")
if err != nil {
t.Fatal("sb.Bind:", err)
}
sc, err = zmq.NewSocket(zmq.PAIR)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = sc.Connect("tcp://127.0.0.1:9737")
if err != nil {
t.Fatal("sc.Connect:", err)
}
poller := zmq.NewPoller()
idxb := poller.Add(sb, 0)
idxc := poller.Add(sc, 0)
if idxb != 0 || idxc != 1 {
t.Errorf("idxb=%d idxc=%d", idxb, idxc)
}
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 1:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 1 len = %d", len(pa))
} else if pa[0].Events != 0 || pa[1].Events != 0 {
t.Errorf("PollAll 1 events = %v, %v", pa[0], pa[1])
}
poller.Update(idxb, zmq.POLLOUT)
poller.UpdateBySocket(sc, zmq.POLLIN)
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 2:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 2 len = %d", len(pa))
} else if pa[0].Events != zmq.POLLOUT || pa[1].Events != 0 {
t.Errorf("PollAll 2 events = %v, %v", pa[0], pa[1])
}
poller.UpdateBySocket(sb, 0)
content := "12345678ABCDEFGH12345678ABCDEFGH"
// Send message from client to server
if rc, err := sb.Send(content, zmq.DONTWAIT); err != nil {
t.Error("sb.Send DONTWAIT:", err)
} else if rc != 32 {
t.Error("sb.Send DONTWAIT:", err32)
}
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 3:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 3 len = %d", len(pa))
} else if pa[0].Events != 0 || pa[1].Events != zmq.POLLIN {
t.Errorf("PollAll 3 events = %v, %v", pa[0], pa[1])
}
// Receive message
if msg, err := sc.Recv(zmq.DONTWAIT); err != nil {
t.Error("sb.Recv DONTWAIT:", err)
} else if msg != content {
t.Error("sb.Recv msg != content")
}
poller.UpdateBySocket(sb, zmq.POLLOUT)
poller.Update(idxc, zmq.POLLIN)
if pa, err := poller.PollAll(100 * time.Millisecond); err != nil {
t.Error("PollAll 4:", err)
} else if len(pa) != 2 {
t.Errorf("PollAll 4 len = %d", len(pa))
} else if pa[0].Events != zmq.POLLOUT || pa[1].Events != 0 {
t.Errorf("PollAll 4 events = %v, %v", pa[0], pa[1])
}
err = sc.Close()
sc = nil
if err != nil {
t.Error("sc.Close:", err)
//.........这里部分代码省略.........