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.
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
//go:build client
|
|
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/noahlann/nnet/pkg/nnet"
|
|
)
|
|
|
|
// This example demonstrates client interacting with a session file server.
|
|
// Usage:
|
|
//
|
|
// go run ./examples/session_file/client.go -addr tcp://127.0.0.1:8083 -cmd set
|
|
// go run ./examples/session_file/client.go -addr tcp://127.0.0.1:8083 -cmd get
|
|
// go run ./examples/session_file/client.go -addr tcp://127.0.0.1:8083 -cmd counter
|
|
func main() {
|
|
addr := flag.String("addr", "tcp://127.0.0.1:8083", "server address")
|
|
cmd := flag.String("cmd", "get", "command to send (set, get, counter)")
|
|
timeout := flag.Duration("timeout", 3*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(*cmd), *timeout)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "request error: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("response: %s\n", string(resp))
|
|
}
|
|
|