You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
992 B
Go
44 lines
992 B
Go
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/noahlann/nnet/pkg/nnet"
|
|
)
|
|
|
|
// This example demonstrates the client Request/Response API with timeout.
|
|
// Usage:
|
|
//
|
|
// go run ./examples/client_request -addr tcp://127.0.0.1:6995 -data "ping" -timeout 2s
|
|
func main() {
|
|
addr := flag.String("addr", "tcp://127.0.0.1:6995", "server address")
|
|
data := flag.String("data", "ping", "request payload")
|
|
timeout := flag.Duration("timeout", 2*time.Second, "request timeout")
|
|
flag.Parse()
|
|
|
|
cfg := &nnet.ClientConfig{
|
|
Addr: *addr,
|
|
ConnectTimeout: 3 * time.Second,
|
|
ReadTimeout: 3 * time.Second,
|
|
WriteTimeout: 3 * time.Second,
|
|
}
|
|
c := nnet.NewClient(cfg)
|
|
defer c.Close()
|
|
|
|
if err := c.Connect(); err != nil {
|
|
log.Fatalf("connect failed: %v", err)
|
|
}
|
|
defer c.Disconnect()
|
|
|
|
resp, err := c.Request([]byte(*data), *timeout)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "request error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
fmt.Printf("response: %s\n", string(resp))
|
|
}
|