pebbe / zmq4

A Go interface to ZeroMQ version 4
BSD 2-Clause "Simplified" License
1.17k stars 163 forks source link

Doesn't pub-sub pattern work in same process? #58

Closed ensonmj closed 9 years ago

ensonmj commented 9 years ago

I modify wuserver.go to test pub-sub in same process, and catch net packets with wireshark. The socket can be established, but sub can't receive any message? how can I make it works?

package main

import (
    zmq "github.com/pebbe/zmq4"

    "fmt"
    "math/rand"
    "time"
)

func main() {
    //  Prepare our publisher
    publisher, _ := zmq.NewSocket(zmq.PUB)
    defer publisher.Close()
    publisher.Bind("tcp://*:5556")
    //publisher.Bind("ipc://weather.ipc")

    sub, _ := zmq.NewSocket(zmq.SUB)
    sub.Connect("tcp://localhost:5556")
    go func() {
        for {
            fmt.Println("sub")
            msg, _ := sub.Recv(0)
            fmt.Println(msg)
            time.Sleep(time.Second)
        }
    }()

    //  Initialize random number generator
    rand.Seed(time.Now().UnixNano())

    // loop for a while aparently
    go func() {
        for {
            //  Get values that will fool the boss
            zipcode := rand.Intn(100000)
            temperature := rand.Intn(215) - 80
            relhumidity := rand.Intn(50) + 10

            //  Send message to all subscribers
            msg := fmt.Sprintf("%05d %d %d", zipcode, temperature, relhumidity)
            fmt.Println("publish: %s", msg)
            publisher.Send(msg, zmq.DONTWAIT)
            time.Sleep(time.Second)
        }
    }()

    for {

    }
}
pebbe commented 9 years ago

Please post questions on how to use ZeroMQ to the mailing list. http://zeromq.org/docs:mailing-lists