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

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

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/

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:

确定