ZeroMQ在Go语言中无法打印我从PULL套接字接收到的JSON消息字节。

huangapple go评论138阅读模式
英文:

ZeroMQ in go won't print my json message bytes received from PULL socket

问题

我正在尝试一个简单的代码:

  1. package main
  2. import (
  3. "fmt"
  4. zmq "github.com/alecthomas/gozmq"
  5. )
  6. func main() {
  7. context, _ := zmq.NewContext()
  8. defer context.Close()
  9. // Socket to receive messages on
  10. receiver, _ := context.NewSocket(zmq.PULL)
  11. defer receiver.Close()
  12. receiver.Connect("tcp://localhost:5557")
  13. // Process tasks forever
  14. for {
  15. msgbytes, _ := receiver.Recv(0)
  16. fmt.Println("received")
  17. fmt.Println(string(msgbytes))
  18. }
  19. }

在Node.js中,我这样发送消息:

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

我可以在控制台看到JSON,所以sender.send()实际上是在发送东西。此外,每个payload的.go程序的输出是:

  1. received
  2. []
  3. received
  4. []

没有输出。我在[GoDocs][1]中搜索了Recv方法,发现没有像其他语言中的recv_json、recv_message等分离,它们都是字节。那么发生了什么?我发送的是字符串,因为它被发送为字符串化,对吗?

更新

如Nehal在下面所说,我将导入语句更改为官方的库,并且这是新代码:

  1. package main
  2. import (
  3. "fmt"
  4. zmq "gopkg.in/zeromq/goczmq.v4"
  5. )
  6. func main() {
  7. // Socket to receive messages on
  8. receiver, _ := zmq.NewPull("tcp://*:5557")
  9. defer receiver.Destroy()
  10. // Process tasks forever
  11. for {
  12. request, _ := receiver.RecvMessage()
  13. fmt.Println("received")
  14. fmt.Println(request)
  15. }
  16. }

但这次连received都没有打印出来,似乎根本没有接收到任何消息。
[1]: https://godoc.org/github.com/zeromq/goczmq

英文:

I'm trying a simple code:

  1. package main
  2. import (
  3. "fmt"
  4. zmq "github.com/alecthomas/gozmq"
  5. )
  6. func main() {
  7. context, _ := zmq.NewContext()
  8. defer context.Close()
  9. // Socket to receive messages on
  10. receiver, _ := context.NewSocket(zmq.PULL)
  11. defer receiver.Close()
  12. receiver.Connect("tcp://localhost:5557")
  13. // Process tasks forever
  14. for {
  15. msgbytes, _ := receiver.Recv(0)
  16. fmt.Println("received")
  17. fmt.Println(string(msgbytes))
  18. }
  19. }

In NodeJS I send messages like this:

  1. console.log(payload);
  2. 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:

  1. received
  2. []
  3. received
  4. []

There's no output. I've searched the [GoDocs][1] 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

  1. import (
  2. "fmt"
  3. zmq "gopkg.in/zeromq/goczmq.v4"
  4. )
  5. func main() {
  6. // Socket to receive messages on
  7. receiver, _ := zmq.NewPull("tcp://*:5557")
  8. defer receiver.Destroy()
  9. // Process tasks forever
  10. for {
  11. request, _ := receiver.RecvMessage()
  12. fmt.Println("received")
  13. fmt.Println(request)
  14. }
  15. }

But this time 'received' isn't even printed, it seems that no message is being received at all
[1]: https://godoc.org/github.com/zeromq/goczmq

答案1

得分: 0

在Go中的服务器代码:

  1. import (
  2. "fmt"
  3. zmq "gopkg.in/zeromq/goczmq.v4"
  4. )
  5. func main() {
  6. // Socket to receive messages on
  7. receiver, err := zmq.NewPull("tcp://*:5557")
  8. if err != nil {
  9. panic(err)
  10. }
  11. defer receiver.Destroy()
  12. // Process tasks forever
  13. for {
  14. request, err := receiver.RecvMessage()
  15. if err != nil {
  16. panic(err)
  17. }
  18. fmt.Printf("Received: '%s'\n", request)
  19. }
  20. }

在Node.js中的客户端代码:

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

服务器端:

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

客户端端:

  1. $ node a.js
  2. Sending data
  3. Sending data
  4. ...

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

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

Go中的优秀zmq示例:https://github.com/booksbyus/zguide/tree/master/examples/Go

入门教程:http://taotetek.github.io/oldschool.systems/post/goczmq1/

英文:

Server in go:

  1. import (
  2. "fmt"
  3. zmq "gopkg.in/zeromq/goczmq.v4"
  4. )
  5. func main() {
  6. // Socket to receive messages on
  7. receiver, err := zmq.NewPull("tcp://*:5557")
  8. if err != nil {
  9. panic(err)
  10. }
  11. defer receiver.Destroy()
  12. // Process tasks forever
  13. for {
  14. request, err := receiver.RecvMessage()
  15. if err != nil {
  16. panic(err)
  17. }
  18. fmt.Printf("Received: '%s'\n", request)
  19. }
  20. }

Client in Node.js:

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

Server Side:

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

Client Side:

  1. $ node a.js
  2. Sending data
  3. Sending data
  4. ...

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/

huangapple
  • 本文由 发表于 2017年2月8日 01:29:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/42096226.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定