dougui4325
2017-02-07 17:29
浏览 167
已采纳

进行中的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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试一个简单的代码:</ p>

  package main  
 
import(
“ fmt” 
 zmq“ github.com/alecthomas/gozmq"
)

func main(){
 context,_:= zmq.NewContext()
延迟上下文。  Close()
 
 //用于在接收器上接收消息的套接字,_:= context.NewSocket(zmq.PULL)
延迟接收器。Close()
接收器。Connect(“ tcp:// localhost:  5557“)
 
 //永远处理任务
,{
 msgbytes,_:=接收器。Recv(0)
 fmt.Println(” received“)
 fmt.Println(string(msgbytes))  
} 
} 
 </ code> </ pre> 
 
 

在NodeJS中,我发送如下消息:</ p>

  console.log(有效负载 ); 
sender.send(JSON.stringify(payload)); 
 </ code> </ pre> 
 
 

我可以在控制台中看到json,所以 sender.sen() </ code>实际上是在发送东西。 此外, .go </ code>程序对每个有效负载的输出为:</ p>

 已接收
 [] 
已接收
 [] 
 </ 代码> </ pre> 
 
 

没有输出。 我已经在 GoDocs 中搜索了 Recv </ code>方法, 没有像其他语言那样的 recv_json,recv_message等</ code>等分隔符,它们都是字节。 那是怎么回事? 我正在发送字符串,因为它是作为stringfy发送的,对吗? </ p>

更新</ strong> </ p>

正如Nehal在下面说的,我将import声明更改为正式代表,这是新的 代码:</ p>

包主程序</ p>

 导入(
“ fmt” 
 zmq“ gopkg.in/zeromq/goczmq.v4”  
)
 
func main(){
 //用于在
接收器上接收消息的套接字,_:= zmq.NewPull(“ tcp:// *:5557”)
延迟接收器。Destroy()\  n 
 //永远处理任务
 for {
 request,_:= receiver.RecvMessage()
 fmt.Println(“ received”)
 fmt.Println(request)
} 
} 
  </ code> </ pre> 
 
 

但这一次甚至没有打印出“接收到的”消息,看来根本没有收到任何消息</ p> </ div>

1条回答 默认 最新

相关推荐 更多相似问题