本文整理汇总了Golang中github.com/pebbe/zmq4.Socket.SetPlainServer方法的典型用法代码示例。如果您正苦于以下问题:Golang Socket.SetPlainServer方法的具体用法?Golang Socket.SetPlainServer怎么用?Golang Socket.SetPlainServer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/pebbe/zmq4.Socket
的用法示例。
在下文中一共展示了Socket.SetPlainServer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestSecurityPlain
func TestSecurityPlain(t *testing.T) {
time.Sleep(100 * time.Millisecond)
var handler, server, client *zmq.Socket
defer func() {
for _, s := range []*zmq.Socket{handler} {
if s != nil {
s.SetLinger(0)
s.Close()
}
}
}()
handler, err := zmq.NewSocket(zmq.REP)
if err != nil {
t.Fatal("NewSocket:", err)
}
err = handler.Bind("inproc://zeromq.zap.01")
if err != nil {
t.Fatal("handler.Bind:", err)
}
doHandler := func(state zmq.State) error {
msg, err := handler.RecvMessage(0)
if err != nil {
return err // Terminating
}
version := msg[0]
sequence := msg[1]
// domain := msg[2]
// address := msg[3]
identity := msg[4]
mechanism := msg[5]
username := msg[6]
password := msg[7]
if version != "1.0" {
return errors.New("version != 1.0")
}
if mechanism != "PLAIN" {
return errors.New("mechanism != PLAIN")
}
if identity != "IDENT" {
return errors.New("identity != IDENT")
}
if username == "admin" && password == "password" {
handler.SendMessage(version, sequence, "200", "OK", "anonymous", "")
} else {
handler.SendMessage(version, sequence, "400", "Invalid username or password", "", "")
}
return nil
}
doQuit := func(i interface{}) error {
err := handler.Close()
if err != nil {
t.Error("handler.Close:", err)
}
return errors.New("Quit")
}
quit := make(chan interface{})
reactor := zmq.NewReactor()
reactor.AddSocket(handler, zmq.POLLIN, doHandler)
reactor.AddChannel(quit, 0, doQuit)
go func() {
reactor.Run(100 * time.Millisecond)
quit <- true
}()
defer func() {
quit <- true
<-quit
close(quit)
}()
// Server socket will accept connections
server, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket", err)
}
err = server.SetIdentity("IDENT")
if err != nil {
t.Fatal("server.SetIdentity:", err)
}
err = server.SetPlainServer(1)
if err != nil {
t.Fatal("server.SetPlainServer(1):", err)
}
err = server.Bind("tcp://127.0.0.1:9998")
if err != nil {
t.Fatal("server.Bind")
}
// Check PLAIN security with correct username/password
client, err = zmq.NewSocket(zmq.DEALER)
if err != nil {
t.Fatal("NewSocket:", err)
}
//.........这里部分代码省略.........