push.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package liveBroadcast
  2. import (
  3. "context"
  4. "go-common/library/log"
  5. bm "go-common/library/net/http/blademaster"
  6. xhttp "net/http"
  7. "net/url"
  8. "strconv"
  9. "strings"
  10. )
  11. const (
  12. _liveBroadCastURLAddr = "http://live-dm.bilibili.co:80/dm/1/push"
  13. )
  14. // type liveBroadcastReq struct {
  15. // Ensure int64 `json:"ensure"`
  16. // Cid int64 `json:"cid"`
  17. // }
  18. type liveBroadcastResp struct {
  19. Code int64 `json:"ret"`
  20. }
  21. //PushBroadcast 广播
  22. func (c *Client) PushBroadcast(ctx context.Context, cid int64, ensure int64, msg string) (err error) {
  23. if len([]rune(msg)) > 2000 {
  24. return
  25. }
  26. resp := &liveBroadcastResp{}
  27. cli := bm.NewClient(c.getConf())
  28. param := url.Values{}
  29. param.Set("cid", strconv.FormatInt(cid, 10))
  30. param.Set("ensure", strconv.FormatInt(0, 10))
  31. url := _liveBroadCastURLAddr + "?" + param.Encode()
  32. req, err := xhttp.NewRequest(xhttp.MethodPost, url, strings.NewReader(msg))
  33. if err != nil {
  34. log.Error("[BroadCastError]error:%+v=", err)
  35. return err
  36. }
  37. if err := cli.Do(ctx, req, resp); err != nil {
  38. log.Error("[BroadCastError]error:%+v=", err)
  39. return err
  40. }
  41. if resp.Code != 1 {
  42. log.Error("BroadCastError] errorcode:%d", resp.Code)
  43. }
  44. return
  45. }