-
-
Save ashayh/12f731c3fbe69037ac3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"time" | |
"github.com/gdamore/mangos" | |
"github.com/gdamore/mangos/protocol/pub" | |
"github.com/gdamore/mangos/protocol/sub" | |
"github.com/gdamore/mangos/transport/ipc" | |
"github.com/gdamore/mangos/transport/tcp" | |
) | |
func die(format string, v ...interface{}) { | |
fmt.Fprintln(os.Stderr, fmt.Sprintf(format, v...)) | |
os.Exit(1) | |
} | |
func date() string { | |
return time.Now().Format(time.ANSIC) | |
} | |
func server(url string) { | |
var sock mangos.Socket | |
var err error | |
if sock, err = pub.NewSocket(); err != nil { | |
die("can't get new pub socket: %s", err) | |
} | |
sock.AddTransport(ipc.NewTransport()) | |
sock.AddTransport(tcp.NewTransport()) | |
if err = sock.Listen(url); err != nil { | |
die("can't listen on pub socket: %s", err.Error()) | |
} | |
for { | |
// Could also use sock.RecvMsg to get header | |
d := date() | |
fmt.Printf("SERVER: PUBLISHING DATE %s\n", d) | |
if err = sock.Send([]byte(d)); err != nil { | |
die("Failed publishing: %s", err.Error()) | |
} | |
time.Sleep(time.Second) | |
} | |
} | |
func client(url string, name string) { | |
var sock mangos.Socket | |
var err error | |
var msg []byte | |
if sock, err = sub.NewSocket(); err != nil { | |
die("can't get new sub socket: %s", err.Error()) | |
} | |
sock.AddTransport(ipc.NewTransport()) | |
sock.AddTransport(tcp.NewTransport()) | |
if err = sock.Dial(url); err != nil { | |
die("can't dial on sub socket: %s", err.Error()) | |
} | |
// Empty byte array effectively subscribes to everything | |
err = sock.SetOption(mangos.OptionSubscribe, []byte("")) | |
if err != nil { | |
die("cannot subscribe: %s", err.Error()) | |
} | |
for { | |
if msg, err = sock.Recv(); err != nil { | |
die("Cannot recv: %s", err.Error()) | |
} | |
fmt.Printf("CLIENT(%s): RECEIVED %s\n", name, string(msg)) | |
} | |
} | |
func main() { | |
if len(os.Args) > 2 && os.Args[1] == "server" { | |
server(os.Args[2]) | |
os.Exit(0) | |
} | |
if len(os.Args) > 3 && os.Args[1] == "client" { | |
client(os.Args[2], os.Args[3]) | |
os.Exit(0) | |
} | |
fmt.Fprintf(os.Stderr, "Usage: pubsub server|client <URL> <ARG>\n") | |
os.Exit(1) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment