Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add hello example #121

Merged
merged 6 commits into from Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions example/hwclient.go
@@ -0,0 +1,51 @@
// Copyright 2022 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

// Hello client
package main

import (
"context"
"fmt"
"log"
"time"

zmq "github.com/go-zeromq/zmq4"
)

func main() {
if err := hwclient(); err != nil {
log.Fatalf("hwclient: %v", err)
}
}

func hwclient() error {
ctx := context.Background()
socket := zmq.NewReq(ctx, zmq.WithDialerRetry(time.Second))
defer socket.Close()

fmt.Printf("Connecting to hello world server...")
if err := socket.Dial("tcp://localhost:5555"); err != nil {
return fmt.Errorf("dialing: %w", err)
}

for i := 0; i < 10; i++ {
// Send hello.
m := zmq.NewMsgString("hello")
fmt.Println("sending ", m)
if err := socket.Send(m); err != nil {
return fmt.Errorf("sending: %w", err)
}

// Wait for reply.
r, err := socket.Recv()
if err != nil {
return fmt.Errorf("receiving: %w", err)
}
fmt.Println("received ", r.String())
}
return nil
}
50 changes: 50 additions & 0 deletions example/hwserver.go
@@ -0,0 +1,50 @@
// Copyright 2022 The go-zeromq Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

// Hello server
package main

import (
"context"
"fmt"
"log"
"time"

zmq "github.com/go-zeromq/zmq4"
)

func main() {
if err := hwserver(); err != nil {
log.Fatalf("hwserver: %w", err)
}
}

func hwserver() error {
ctx := context.Background()
// Socket to talk to clients
socket := zmq.NewRep(ctx)
defer socket.Close()
if err := socket.Listen("tcp://*:5555"); err != nil {
return fmt.Errorf("listening: %w", err)
}

for {
msg, err := socket.Recv()
if err != nil {
return fmt.Errorf("receiving: %w", err)
}
fmt.Println("Received ", msg)

// Do some 'work'
time.Sleep(time.Second)

reply := fmt.Sprintf("World")
if err := socket.Send(zmq.NewMsgString(reply)); err != nil {
return fmt.Errorf("sending reply: %w", err)
}
}
}