You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

push.go 2.0 KiB

1 month ago
1 month ago
1 month ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package service
  2. import (
  3. "context"
  4. "egg-im/pkg/gerrors"
  5. "egg-im/pkg/grpclib"
  6. "egg-im/pkg/logger"
  7. "egg-im/pkg/mq"
  8. "egg-im/pkg/pb"
  9. "egg-im/pkg/util"
  10. "time"
  11. "go.uber.org/zap"
  12. "google.golang.org/protobuf/proto"
  13. )
  14. type pushService struct{}
  15. var PushService = new(pushService)
  16. // PushToUser 向用户推送消息
  17. func (s *pushService) PushToUser(ctx context.Context, userId int64, code pb.PushCode, message proto.Message, isPersist bool) error {
  18. logger.Logger.Debug("push_to_user",
  19. zap.Int64("request_id", grpclib.GetCtxRequestId(ctx)),
  20. zap.Int64("user_id", userId),
  21. zap.Int32("code", int32(code)),
  22. zap.Any("message", message))
  23. messageBuf, err := proto.Marshal(message)
  24. if err != nil {
  25. return gerrors.WrapError(err)
  26. }
  27. commandBuf, err := proto.Marshal(&pb.Command{Code: int32(code), Data: messageBuf})
  28. if err != nil {
  29. return gerrors.WrapError(err)
  30. }
  31. _, err = MessageService.SendToUser(ctx,
  32. &pb.Sender{
  33. SenderType: pb.SenderType_ST_SYSTEM,
  34. SenderId: 0,
  35. },
  36. userId,
  37. &pb.SendMessageReq{
  38. ReceiverType: pb.ReceiverType_RT_USER,
  39. ReceiverId: userId,
  40. ToUserIds: nil,
  41. MessageType: pb.MessageType_MT_COMMAND,
  42. MessageContent: commandBuf,
  43. SendTime: util.UnixMilliTime(time.Now()),
  44. IsPersist: isPersist,
  45. },
  46. )
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. // PushAll 全服推送
  53. func (s *pushService) PushAll(ctx context.Context, req *pb.PushAllReq) error {
  54. msg := pb.PushAllMsg{
  55. MessageSend: &pb.MessageSend{
  56. Message: &pb.Message{
  57. Sender: &pb.Sender{SenderType: pb.SenderType_ST_BUSINESS},
  58. ReceiverType: pb.ReceiverType_RT_ROOM,
  59. ToUserIds: nil,
  60. MessageType: req.MessageType,
  61. MessageContent: req.MessageContent,
  62. Seq: 0,
  63. SendTime: util.UnixMilliTime(time.Now()),
  64. Status: 0,
  65. },
  66. },
  67. }
  68. bytes, err := proto.Marshal(&msg)
  69. if err != nil {
  70. return gerrors.WrapError(err)
  71. }
  72. err = mq.Publish(mq.PushAllTopic, bytes)
  73. if err != nil {
  74. return err
  75. }
  76. return nil
  77. }