英文:
ZeroMQ in go won't print my json message bytes received from PULL socket
问题
我正在尝试一个简单的代码:
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))
}
}
在Node.js中,我这样发送消息:
console.log(payload);
sender.send(JSON.stringify(payload));
我可以在控制台看到JSON,所以sender.send()
实际上是在发送东西。此外,每个payload的.go
程序的输出是:
received
[]
received
[]
没有输出。我在[GoDocs][1]中搜索了Recv
方法,发现没有像其他语言中的recv_json、recv_message
等分离,它们都是字节。那么发生了什么?我发送的是字符串,因为它被发送为字符串化,对吗?
更新
如Nehal在下面所说,我将导入语句更改为官方的库,并且这是新代码:
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)
}
}
但这次连received
都没有打印出来,似乎根本没有接收到任何消息。
[1]: https://godoc.org/github.com/zeromq/goczmq
英文:
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][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
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]: https://godoc.org/github.com/zeromq/goczmq
答案1
得分: 0
在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'\n", request)
}
}
在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);
服务器端:
$ go run a.go
Received: '[{"msg":"Hi There!"}]'
Received: '[{"msg":"Hi There!"}]'
...
客户端端:
$ node a.js
Sending data
Sending data
...
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:
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'\n", 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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论