mirror of
https://github.com/statbate/special-pancake.git
synced 2026-08-11 03:12:42 +00:00
complete
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
parent
8439348176
commit
d9e6c4edbc
1 changed files with 79 additions and 23 deletions
102
test3/main.go
102
test3/main.go
|
|
@ -11,49 +11,105 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ctx := context.Background()
|
if len(os.Args) < 3 {
|
||||||
|
fmt.Println("./test room_uid authToken")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
room_uid := os.Args[1]
|
||||||
|
authToken := os.Args[2]
|
||||||
|
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
c, err := ably.NewRealtime(
|
c, err := ably.NewRealtime(
|
||||||
ably.WithAutoConnect(true),
|
ably.WithAutoConnect(true),
|
||||||
ably.WithToken(os.Args[1]),
|
ably.WithToken(authToken),
|
||||||
// ably.WithLogLevel(ably.LogDebug),
|
// ably.WithLogLevel(ably.LogDebug),
|
||||||
ably.WithAuthMethod(http.MethodGet),
|
ably.WithAuthMethod(http.MethodGet),
|
||||||
ably.WithUseTokenAuth(true),
|
ably.WithUseTokenAuth(true),
|
||||||
ably.WithRealtimeHost("realtime.pa.highwebmedia.com"),
|
ably.WithRealtimeHost("realtime.pa.highwebmedia.com"),
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
fmt.Println(err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
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 {
|
type donateMsg struct {
|
||||||
Name string `json:"to_username"`
|
Name string `json:"to_username"`
|
||||||
From string `json:"from_username"`
|
From string `json:"from_username"`
|
||||||
Amount int64 `json:"amount"`
|
Amount int64 `json:"amount"`
|
||||||
}
|
}
|
||||||
|
|
||||||
cancel, err := rc.SubscribeAll(ctx, func(msg *ably.Message) {
|
type leaveMsg struct {
|
||||||
donate := &donateMsg{}
|
Action string `json:"action"`
|
||||||
if err := json.Unmarshal([]byte(msg.Data.(string)), donate); err != nil {
|
IsBroadcaster bool `json:"is_broadcaster"`
|
||||||
|
}
|
||||||
|
|
||||||
|
tipChannel := "room:tip_alert:" + room_uid
|
||||||
|
leaveChannel := "room:enter_leave:" + room_uid
|
||||||
|
|
||||||
|
handler := func(msg *ably.Message) {
|
||||||
|
switch msg.Name {
|
||||||
|
case leaveChannel:
|
||||||
|
leave := &leaveMsg{}
|
||||||
|
if err := json.Unmarshal([]byte(msg.Data.(string)), leave); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if leave.Action == "leave" && leave.IsBroadcaster {
|
||||||
|
fmt.Println("user finished and exit")
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case tipChannel:
|
||||||
|
donate := &donateMsg{}
|
||||||
|
if err := json.Unmarshal([]byte(msg.Data.(string)), donate); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
cancel()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if donate.From == "" {
|
||||||
|
donate.From = "anonymous"
|
||||||
|
}
|
||||||
|
fmt.Println(donate.From, "send ", donate.Amount, "tokens")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, channel := range []string{tipChannel, leaveChannel} {
|
||||||
|
rc := c.Channels.Get(channel)
|
||||||
|
defer func() {
|
||||||
|
if err := c.Channels.Release(ctx, channel); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
cancel()
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := rc.Attach(ctx); err != nil {
|
||||||
fmt.Println(err.Error())
|
fmt.Println(err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(donate.From) > 3 {
|
/* not needed as used Release before
|
||||||
fmt.Println(donate.From, "send ", donate.Amount, "tokens")
|
defer func() {
|
||||||
|
if err := rc.Detach(ctx); err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
*/
|
||||||
|
|
||||||
|
rccancel, err := rc.SubscribeAll(ctx, handler)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println(err.Error())
|
||||||
|
cancel()
|
||||||
}
|
}
|
||||||
})
|
defer rccancel()
|
||||||
if err != nil {
|
ecancel := rc.On(ably.ChannelEventDetached, func(state ably.ChannelStateChange) {
|
||||||
panic(err)
|
fmt.Printf("received detached")
|
||||||
|
cancel()
|
||||||
|
})
|
||||||
|
defer ecancel()
|
||||||
}
|
}
|
||||||
defer cancel()
|
|
||||||
defer c.Close()
|
defer c.Close()
|
||||||
select {}
|
<-ctx.Done()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue