From 8439348176f1d3a28c0e25c310d29aff9cb4e1b2 Mon Sep 17 00:00:00 2001 From: Vasiliy Tolstov Date: Wed, 25 Jan 2023 00:48:08 +0300 Subject: [PATCH] use ably Signed-off-by: Vasiliy Tolstov --- test3/main.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 test3/main.go diff --git a/test3/main.go b/test3/main.go new file mode 100644 index 0000000..67d8c61 --- /dev/null +++ b/test3/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "os" + + ably "github.com/ably/ably-go/ably" +) + +func main() { + ctx := context.Background() + c, err := ably.NewRealtime( + ably.WithAutoConnect(true), + ably.WithToken(os.Args[1]), + // ably.WithLogLevel(ably.LogDebug), + ably.WithAuthMethod(http.MethodGet), + ably.WithUseTokenAuth(true), + ably.WithRealtimeHost("realtime.pa.highwebmedia.com"), + ) + if err != nil { + panic(err) + } + + rc := c.Channels.Get("room:tip_alert:" + os.Args[2]) + if err := rc.Attach(ctx); err != nil { + panic(err) + } + defer func() { + if err := rc.Detach(ctx); err != nil { + panic(err) + } + }() + + type donateMsg struct { + Name string `json:"to_username"` + From string `json:"from_username"` + Amount int64 `json:"amount"` + } + + cancel, err := rc.SubscribeAll(ctx, func(msg *ably.Message) { + donate := &donateMsg{} + if err := json.Unmarshal([]byte(msg.Data.(string)), donate); err != nil { + fmt.Println(err.Error()) + return + } + if len(donate.From) > 3 { + fmt.Println(donate.From, "send ", donate.Amount, "tokens") + } + }) + if err != nil { + panic(err) + } + defer cancel() + defer c.Close() + select {} +}