zeromq / goczmq

goczmq is a golang wrapper for CZMQ.
Mozilla Public License 2.0
591 stars 94 forks source link

Simple Req example? #298

Closed tgbv closed 3 years ago

tgbv commented 3 years ago

I'm using gopkg.in/zeromq/goczmq.v4

I need to send a Req to a Node ZMQ Res server. Assuming the server is started.

I've tested 3 methods which made sense to me, all of them returned error:

package main

import (
    "log"
    "time"

    "gopkg.in/zeromq/goczmq.v4"
)

func main() {
    sock, err := goczmq.NewReq("tcp://127.0.0.1:81")
    if err != nil {
        panic(err)
    }

    // All returned error
    _, err := sock.Write([]byte("message"))
    //err := sock.SendFrame([]byte("message"))
    //err := sock.SendMessage([][]byte{[]byte("message")})
    if err != nil {
        panic(err)
    }
}

panic: send frame error

How do I properly send a simple req? What am I missing?

tgbv commented 3 years ago

A Req requires a response:

package main

import (
    "log"

    "gopkg.in/zeromq/goczmq.v4"
)

func main() {
    // New socket for request
    sock, err := goczmq.NewReq("tcp://127.0.0.1:81")
    if err != nil {
        panic(err)
    }

    // send message to Response server
    err = sock.SendFrame([]byte("message"), 0)
    if err != nil {
        panic(err)
    }
    log.Print("sent")

    // make slice for response and wait for it from Response server
    res := make([]byte, 32)
    i, err := sock.Read(res)
    if err != nil {
        panic(err)
    }
    log.Print("read", i, res)
}