From c71e731b4c31a942ec699e2930a3ebd803c98f3b Mon Sep 17 00:00:00 2001
From: hozan23 <hozan23@karyontech.net>
Date: Thu, 13 Jun 2024 14:49:09 +0200
Subject: add an example

---
 example.go                           | 70 ++++++++++++++++++++++++++++++++++++
 jsonrpc/client/client.go             |  4 +--
 jsonrpc/client/message_dispatcher.go |  4 +--
 3 files changed, 74 insertions(+), 4 deletions(-)
 create mode 100644 example.go

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)
+	}
+}
diff --git a/jsonrpc/client/client.go b/jsonrpc/client/client.go
index a34828a..67ed307 100644
--- a/jsonrpc/client/client.go
+++ b/jsonrpc/client/client.go
@@ -32,8 +32,8 @@ type RPCClientConfig struct {
 type RPCClient struct {
 	config      RPCClientConfig
 	conn        *websocket.Conn
-	requests    messageDispatcher[message.RequestID, message.Response]
-	subscriber  messageDispatcher[message.SubscriptionID, json.RawMessage]
+	requests    *messageDispatcher[message.RequestID, message.Response]
+	subscriber  *messageDispatcher[message.SubscriptionID, json.RawMessage]
 	stop_signal chan struct{}
 }
 
diff --git a/jsonrpc/client/message_dispatcher.go b/jsonrpc/client/message_dispatcher.go
index 6484484..dab75ca 100644
--- a/jsonrpc/client/message_dispatcher.go
+++ b/jsonrpc/client/message_dispatcher.go
@@ -14,9 +14,9 @@ type messageDispatcher[K comparable, V any] struct {
 }
 
 // newMessageDispatcher Creates a new messageDispatcher
-func newMessageDispatcher[K comparable, V any](bufferSize int) messageDispatcher[K, V] {
+func newMessageDispatcher[K comparable, V any](bufferSize int) *messageDispatcher[K, V] {
 	chans := make(map[K]chan<- V)
-	return messageDispatcher[K, V]{
+	return &messageDispatcher[K, V]{
 		chans:      chans,
 		bufferSize: bufferSize,
 	}
-- 
cgit v1.2.3