diff options
author | hozan23 <119854621+hozan23@users.noreply.github.com> | 2024-06-13 14:51:54 +0200 |
---|---|---|
committer | hozan23 <hozan23@karyontech.net> | 2024-06-13 15:04:32 +0200 |
commit | e9af9bc115e0869570b9b79e1610b2bc08abe5a1 (patch) | |
tree | 570a4797d8b18f3ac6f92e24e5416839283d482d /example_test.go | |
parent | c71e731b4c31a942ec699e2930a3ebd803c98f3b (diff) |
Create .github/workflows/go.yml
Diffstat (limited to 'example_test.go')
-rw-r--r-- | example_test.go | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/example_test.go b/example_test.go new file mode 100644 index 0000000..ca4a642 --- /dev/null +++ b/example_test.go @@ -0,0 +1,70 @@ +package karyongo + +import ( + "encoding/json" + "math/rand/v2" + "os" + "time" + + rpc "github.com/karyontech/karyon-go/jsonrpc/client" + log "github.com/sirupsen/logrus" +) + +type Pong struct{} + +func runNewClient() error { + config := rpc.RPCClientConfig{ + Addr: "ws://127.0.0.1:6000", + } + + client, err := rpc.NewRPCClient(config) + if err != nil { + return err + } + defer client.Close() + + subID, ch, err := client.Subscribe("Calc.log_subscribe", nil) + if err != nil { + return err + } + log.Infof("Subscribed successfully: %d\n", subID) + + go func() { + for notification := range ch { + log.Infof("Receive new notification: %s\n", notification) + } + }() + + for { + millisecond := rand.IntN(2000-500) + 500 + time.Sleep(time.Duration(millisecond) * time.Millisecond) + result, err := client.Call("Calc.ping", nil) + if err != nil { + return err + } + + pongMsg := Pong{} + err = json.Unmarshal(*result, &pongMsg) + if err != nil { + return err + } + } + +} + +func main() { + lvl, ok := os.LookupEnv("LOG_LEVEL") + if !ok { + lvl = "debug" + } + ll, err := log.ParseLevel(lvl) + if err != nil { + ll = log.DebugLevel + } + log.SetLevel(ll) + + err = runNewClient() + if err != nil { + log.Fatal(err) + } +} |