本文整理匯總了Golang中github.com/gdamore/mangos.Socket類的典型用法代碼示例。如果您正苦於以下問題:Golang Socket類的具體用法?Golang Socket怎麽用?Golang Socket使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Socket類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: NewPublisher
func NewPublisher(ctx context.Context, url string) (*Publisher, error) {
var sock mangos.Socket
var err error
sock, err = pub.NewSocket()
if err != nil {
return nil, err
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
err = sock.Listen(url)
if err != nil {
return nil, err
}
publiser := &Publisher{
ctx: ctx,
url: url,
sock: sock,
publishCh: make(chan []string),
}
go publiser.run()
return publiser, nil
}
示例2: NewListener
func (t *inprocTran) NewListener(addr string, sock mangos.Socket) (mangos.PipeListener, error) {
if _, err := mangos.StripScheme(t, addr); err != nil {
return nil, err
}
l := &listener{addr: addr, proto: sock.GetProtocol()}
return l, nil
}
示例3: send
func send(s mangos.Socket, obj interface{}) error {
data, err := json.Marshal(obj)
if err != nil {
return stackerr.Wrap(err)
}
return stackerr.Wrap(s.Send(data))
}
示例4: replyLoop
func replyLoop(sock mangos.Socket, done chan struct{}) {
defer close(done)
if sendData == nil {
fatalf("No data to send!")
}
for {
msg, err := sock.RecvMsg()
switch err {
case mangos.ErrRecvTimeout:
return
case nil:
default:
fatalf("RecvMsg failed: %v", err)
}
printMsg(msg)
msg.Free()
msg = mangos.NewMessage(len(sendData))
msg.Body = append(msg.Body, sendData...)
err = sock.SendMsg(msg)
if err != nil {
fatalf("SendMsg failed: %v", err)
}
}
}
示例5: recv
func recv(s mangos.Socket, obj interface{}) error {
data, err := s.Recv()
if err != nil {
return stackerr.Wrap(err)
}
return stackerr.Wrap(json.Unmarshal(data, obj))
}
示例6: recv_name
func recv_name(sock mangos.Socket, name string) {
var msg []byte
var err error
if msg, err = sock.Recv(); err == nil {
fmt.Printf("%s: RECEIVED: \"%s\"\n", name, string(msg))
}
}
示例7: sendRequest
func sendRequest(s mangos.Socket, request request) (*response, error) {
fmt.Println("Sending a request")
requestJSON, err := json.Marshal(request)
if err != nil {
// This is not recoverable.
panic(err)
}
fmt.Printf("sendRequest: %s\n", requestJSON)
if err := s.Send(requestJSON); err != nil {
return nil, err
}
rep, err := s.Recv()
if err != nil {
return nil, err
}
var resp response
if err := json.Unmarshal(rep, &resp); err != nil {
return nil, err
}
return &resp, nil
}
示例8: send_recv
func send_recv(sock mangos.Socket, name string) {
for {
sock.SetOption(mangos.OptionRecvDeadline, 100*time.Millisecond)
recv_name(sock, name)
time.Sleep(time.Second)
send_name(sock, name)
}
}
示例9: publishEvt
func publishEvt(channel mangos.Socket, evt string) error {
if err := channel.Send([]byte(evt)); err != nil {
log.Printf("fail to publish '%v' event:%v\n", evt, err)
return err
}
return nil
}
示例10: output
func output(in chan string, out mangos.Socket) {
if out == nil {
panic("Invalid socket")
}
for item := range in {
out.Send([]byte(item))
}
}
示例11: getEvt
func getEvt(channel mangos.Socket) (string, error) {
log.Println("getEvt()")
evt, err := channel.Recv()
if err != nil {
return "", err
}
return string(evt), nil
}
示例12: StartNanomsgProtoServer
func StartNanomsgProtoServer(port string) {
var server mangos.Socket
var err error
if server, err = rep.NewSocket(); err != nil {
log.Fatal(err)
}
server.AddTransport(tcp.NewTransport())
server.Listen("tcp://0.0.0.0:" + port)
for {
msg, _ := server.Recv()
var body pb.Request
proto.Unmarshal(msg, &body)
data, _ := proto.Marshal(&pb.Response{
Method: body.Method,
PayloadLength: int64(len(body.Payload)),
})
server.Send(data)
}
}
示例13: client
func client(url string, name string) {
var sock mangos.Socket
var err error
var msg []byte
if sock, err = sub.NewSocket(); err != nil {
die("can't get new sub socket: %s", err.Error())
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err = sock.Dial(url); err != nil {
die("can't dial on sub socket: %s", err.Error())
}
// Empty byte array effectively subscribes to everything
err = sock.SetOption(mangos.OptionSubscribe, []byte(""))
if err != nil {
die("cannot subscribe: %s", err.Error())
}
for {
if msg, err = sock.Recv(); err != nil {
die("Cannot recv: %s", err.Error())
}
fmt.Printf("CLIENT(%s): RECEIVED %s\n", name, string(msg))
}
}
示例14: StartNanomsgJsonServer
func StartNanomsgJsonServer(port string) {
var server mangos.Socket
var err error
if server, err = rep.NewSocket(); err != nil {
log.Fatal(err)
}
server.AddTransport(tcp.NewTransport())
server.Listen("tcp://0.0.0.0:" + port)
for {
msg, _ := server.Recv()
var body Request
var response []byte
json.Unmarshal(msg, &body)
if response, err = json.Marshal(Response{Method: body.Method, PayloadLength: len(body.Payload)}); err != nil {
log.Fatal(err)
}
server.Send(response)
}
}
示例15: client
func client(url string, name string) {
var sock mangos.Socket
var err error
var msg []byte
if sock, err = respondent.NewSocket(); err != nil {
die("can't get new respondent socket: %s", err.Error())
}
sock.AddTransport(ipc.NewTransport())
sock.AddTransport(tcp.NewTransport())
if err = sock.Dial(url); err != nil {
die("can't dial on respondent socket: %s", err.Error())
}
for {
if msg, err = sock.Recv(); err != nil {
die("Cannot recv: %s", err.Error())
}
fmt.Printf("CLIENT(%s): RECEIVED \"%s\" SURVEY REQUEST\n",
name, string(msg))
d := date()
fmt.Printf("CLIENT(%s): SENDING DATE SURVEY RESPONSE\n", name)
if err = sock.Send([]byte(d)); err != nil {
die("Cannot send: %s", err.Error())
}
}
}