本文整理汇总了Golang中github.com/alecthomas/gozmq.Socket.Connect方法的典型用法代码示例。如果您正苦于以下问题:Golang Socket.Connect方法的具体用法?Golang Socket.Connect怎么用?Golang Socket.Connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/alecthomas/gozmq.Socket
的用法示例。
在下文中一共展示了Socket.Connect方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: runZmqStream
func runZmqStream() {
var context zmq.Context
var socket zmq.Socket
// connect to zmq
var err error
if context, err = zmq.NewContext(); err != nil {
panic("No ZMQ Context?")
}
defer context.Close()
if socket, err = context.NewSocket(zmq.SUB); err != nil {
panic("No ZMQ Socket Outbound??")
}
defer socket.Close()
socket.Connect("tcp://localhost:5558")
socket.SetSockOptString(zmq.SUBSCRIBE, "")
for {
// block here, waiting for inbound requests
msg, _ := socket.Recv(0)
if len(msg) > 0 {
parts := strings.Split(string(msg), "\n\n")
process(parts[0])
}
}
}
示例2: watchRep
func watchRep(ctx zmq.Context, global_rep_socket, global_sub_socket zmq.Socket, clients *[]string, pub_address string) {
for {
data, _ := global_rep_socket.Recv(0)
var req Request
_ = json.Unmarshal(data, &req)
if req.Type == "connect" {
var creq ConnRequest
json.Unmarshal(data, &creq)
if !strSliceContains(*clients, creq.RepAddress) {
global_sub_socket.Connect(creq.PubAddress)
reply, _ := json.Marshal(ConnReply{pub_address, *clients})
global_rep_socket.Send(reply, 0)
for _, adr := range *clients {
req_socket := openSocket(ctx, zmq.REQ, adr)
req_socket.Send(data, 0)
req_socket.Close()
}
*clients = append(*clients, creq.RepAddress)
log.Printf("Connected: " + creq.RepAddress)
}
}
runtime.Gosched()
}
}
示例3: ListenAndServe
// the listen and server for mongrel, expects an address like this
// @addr = string config parameter like this:
// m2go.ListenAndServe("tcp://127.0.0.1:9555|tcp://127.0.0.1:9556|54c6755b-9628-40a4-9a2d-cc82a816345e", handler)
func ListenAndServe(addr string, handler http.Handler) {
var Context zmq.Context
var SocketIn zmq.Socket
var SocketOut zmq.Socket
var hasExited bool
var err error
m2addr := strings.Split(addr, "|") //
log.Printf("m2go serving %s\n", addr)
/*
Connection to ZMQ setup
*/
connect := func() {
if Context, err = zmq.NewContext(); err != nil {
panic("No ZMQ Context?")
}
// listen for incoming requests
if SocketIn, err = Context.NewSocket(zmq.PULL); err != nil {
panic("No ZMQ Socket?")
}
SocketIn.Connect(m2addr[0])
if SocketOut, err = Context.NewSocket(zmq.PUB); err != nil {
panic("No ZMQ Socket Outbound??")
}
// outbound response on a different channel
SocketOut.SetSockOptString(zmq.IDENTITY, m2addr[2])
//socket.SetSockOptString(zmq.SUBSCRIBE, filter)
SocketOut.Connect(m2addr[1])
}
connect()
handleResponse := func(response []byte) {
SocketOut.Send(response, 0)
}
stopper := func() {
if !hasExited {
hasExited = true
SocketOut.Close()
SocketIn.Close()
Context.Close()
}
}
defer stopper()
for {
// each inbound request
m2data, err := SocketIn.Recv(0)
//log.Println(string(m2data))
if err != nil {
log.Println("ZMQ Socket Input accept error ", err.Error())
} else {
go HandleM2Request(m2data, handleResponse, handler)
}
}
log.Print("after close of runner")
}