aboutsummaryrefslogtreecommitdiff
path: root/example.go
diff options
context:
space:
mode:
authorhozan23 <hozan23@karyontech.net>2024-06-13 14:49:09 +0200
committerhozan23 <hozan23@karyontech.net>2024-06-13 14:49:09 +0200
commitc71e731b4c31a942ec699e2930a3ebd803c98f3b (patch)
tree4634af20ed620348922577c546dd7d9405a9a26f /example.go
parent8c2d37e093ca64d591fc0aec15a7e2ed424b2e47 (diff)
add an example
Diffstat (limited to 'example.go')
-rw-r--r--example.go70
1 files changed, 70 insertions, 0 deletions
diff --git a/example.go b/example.go
new file mode 100644
index 0000000..b51bc24
--- /dev/null
+++ b/example.go
@@ -0,0 +1,70 @@
+package main
+
+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)
+ }
+}