mirror of
https://github.com/statbate/special-pancake.git
synced 2026-08-11 03:12:42 +00:00
use ably
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
f95ad6ce97
commit
8439348176
1 changed files with 59 additions and 0 deletions
59
test3/main.go
Normal file
59
test3/main.go
Normal 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 {}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue