Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Vasiliy Tolstov 2023-01-25 00:48:08 +03:00
parent f95ad6ce97
commit 8439348176

59
test3/main.go Normal file
View file

@ -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 {}
}