/
broadcast.go
85 lines (76 loc) · 2.03 KB
/
broadcast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package dao
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"go-common/library/log"
"go-common/library/net/metadata"
)
const (
_goimChatURI = "/x/internal/chat/push/room"
_broadcastURI = "/x/internal/broadcast/push/room"
_broadcastDmOperation = 1000
_broadcastDmRoomFmt = "video://%d/%d" //video://{aid}/{cid}
)
func (d *Dao) goimChatURI() string {
return d.conf.Host.API + _goimChatURI
}
func (d *Dao) broadcastURI() string {
return d.conf.Host.API + _broadcastURI
}
// BroadcastInGoim send dm msg in realtime.
func (d *Dao) BroadcastInGoim(c context.Context, cid, aid int64, info json.RawMessage) (err error) {
var (
res struct {
Code int64 `json:"code"`
}
data = map[string]interface{}{
"cmd": "DM",
"info": info,
}
)
v, err := json.Marshal(data)
if err != nil {
log.Error("json.Marshal(%s) error(%v)", info, err)
return
}
url := fmt.Sprintf("%s?cids=%d&aid=%d", d.goimChatURI(), cid, aid)
req, err := http.NewRequest("POST", url, bytes.NewReader(v))
if err != nil {
log.Error("broadcast http.NewRequest() error(%v)", err)
return
}
if err = d.httpCli.Do(c, req, &res); err != nil {
log.Error("httpCli.Do(%s) error(%v)", url, err)
return
}
if res.Code != 0 {
err = fmt.Errorf("broadcast api failed(%d)", res.Code)
log.Error("broadcast(%s) res code(%d)", url, res.Code)
}
return
}
// Broadcast send dm msg in realtime.
func (d *Dao) Broadcast(c context.Context, cid, aid int64, msg string) (err error) {
var (
res struct {
Code int64 `json:"code"`
}
)
params := url.Values{}
params.Set("operation", fmt.Sprint(_broadcastDmOperation))
params.Set("room", fmt.Sprintf(_broadcastDmRoomFmt, aid, cid))
params.Set("message", msg)
if err = d.httpCli.Post(c, d.broadcastURI(), metadata.String(c, metadata.RemoteIP), params, &res); err != nil {
log.Error("httpCli.Do(%s) error(%v)", d.broadcastURI(), err)
return
}
if res.Code != 0 {
err = fmt.Errorf("broadcast api failed(%d)", res.Code)
log.Error("broadcast(%s) res code(%d)", d.broadcastURI(), res.Code)
}
return
}