dougui4325
dougui4325
2017-02-07 17:29
浏览 123

进行中的ZeroMQ不会打印从PULL套接字接收到的json消息字节

I'm trying a simple code:

package main

import (
    "fmt"
    zmq "github.com/alecthomas/gozmq"
)

func main() {
    context, _ := zmq.NewContext()
    defer context.Close()

    //  Socket to receive messages on
    receiver, _ := context.NewSocket(zmq.PULL)
    defer receiver.Close()
    receiver.Connect("tcp://localhost:5557")

    //  Process tasks forever
    for {
        msgbytes, _ := receiver.Recv(0)
    fmt.Println("received")
        fmt.Println(string(msgbytes))
    }
}

In NodeJS I send messages like this:

console.log(payload);
sender.send(JSON.stringify(payload));

I can see the json in the console, so sender.sen() is actually sending things. Also, the output from the .go program for each payload is:

received
[]
received
[]

There's no output. I've searched the GoDocs for the Recv method and there's no separation like recv_json, recv_message, etc like in other languages, it's all bytes. So what's happening? I'm sending a string because it's sent as stringfy, right?

UPDATE

As Nehal said below, I changed the import statement to the official rep, and this is the new code:

package main

import (
    "fmt"
    zmq "gopkg.in/zeromq/goczmq.v4"
)

func main() {
    //  Socket to receive messages on
    receiver, _ := zmq.NewPull("tcp://*:5557")
    defer receiver.Destroy()

    //  Process tasks forever
    for {
        request, _ := receiver.RecvMessage()
    fmt.Println("received")
        fmt.Println(request)
    }
}

But this time 'received' isn't even printed, it seems that no message is being received at all

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongtang1910
    dongtang1910 2017-02-07 20:28
    已采纳

    Server in go:

    import (
        "fmt"
        zmq "gopkg.in/zeromq/goczmq.v4"
    )
    
    func main() {
        //  Socket to receive messages on
        receiver, err := zmq.NewPull("tcp://*:5557")
        if err != nil {
            panic(err)
        }
    
        defer receiver.Destroy()
    
        //  Process tasks forever
        for {
            request, err := receiver.RecvMessage()
            if err != nil {
                panic(err)
            }
            fmt.Printf("Received: '%s'
    ", request)
        }
    }
    

    Client in Node.js:

    var zmq = require('zmq')
      , sock = zmq.socket('push');
    
    sock.connect('tcp://127.0.0.1:5557');
    setInterval(function(){
       console.log('Sending data');
       sock.send(JSON.stringify({'msg': 'Hi There!'}));
    }, 500);
    

    Server Side:

    $ go run a.go
    Received: '[{"msg":"Hi There!"}]'
    Received: '[{"msg":"Hi There!"}]'
    ...
    

    Client Side:

    $ node a.js 
    Sending data
    Sending data
    ...
    

    RecvMessage Documentation: https://godoc.org/github.com/zeromq/goczmq#Sock.RecvMessage

    Node.js package: https://github.com/JustinTulloss/zeromq.node

    Excellent zmq examples in go: https://github.com/booksbyus/zguide/tree/master/examples/Go

    Nice getting started tutorial: http://taotetek.github.io/oldschool.systems/post/goczmq1/

    点赞 评论

相关推荐