msgtransfer.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package paopaoqueue
  2. import (
  3. "context"
  4. "encoding/json"
  5. "git.banshen.xyz/huangguangrong/slow_wild_queue/types"
  6. "github.com/zeromicro/go-queue/kq"
  7. )
  8. type MsgChatTransferClient interface {
  9. Push(msg *types.MsgChatTransfer) error
  10. }
  11. type msgChatTransferClient struct {
  12. pusher *kq.Pusher
  13. }
  14. func NewMsgChatTransferClient(addr []string, topic string, opts ...kq.Pusher) MsgChatTransferClient {
  15. return &msgChatTransferClient{pusher: kq.NewPusher(addr, topic)}
  16. }
  17. func (m *msgChatTransferClient) Push(msg *types.MsgChatTransfer) error {
  18. data, err := json.Marshal(msg)
  19. if err != nil {
  20. return err
  21. }
  22. return m.pusher.Push(context.Background(), string(data))
  23. }
  24. type MsgReadTransferClient interface {
  25. Push(msg *types.MsgMarkRead) error
  26. }
  27. type msgReadTransferClient struct {
  28. pusher *kq.Pusher
  29. }
  30. func NewMsgReadTransferClient(addr []string, topic string, opts ...kq.Pusher) MsgReadTransferClient {
  31. return &msgReadTransferClient{pusher: kq.NewPusher(addr, topic)}
  32. }
  33. func (m *msgReadTransferClient) Push(msg *types.MsgMarkRead) error {
  34. data, err := json.Marshal(msg)
  35. if err != nil {
  36. return err
  37. }
  38. return m.pusher.Push(context.Background(), string(data))
  39. }
  40. // 消息推送
  41. type MsgPushClientTransferClient interface {
  42. Push(msg *types.MessagePushData) error
  43. }
  44. type msgPushClientTransferClient struct {
  45. pusher *kq.Pusher
  46. }
  47. func NewMsgPushClientTransferClient(addr []string, topic string, opts ...kq.Pusher) MsgPushClientTransferClient {
  48. return &msgPushClientTransferClient{pusher: kq.NewPusher(addr, topic)}
  49. }
  50. func (m *msgPushClientTransferClient) Push(msg *types.MessagePushData) error {
  51. data, err := json.Marshal(msg)
  52. if err != nil {
  53. return err
  54. }
  55. return m.pusher.Push(context.Background(), string(data))
  56. }