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.

2 weeks ago
1 week ago
2 weeks ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package connect
  2. import (
  3. "egg-im/config"
  4. "egg-im/pkg/db"
  5. "egg-im/pkg/logger"
  6. "egg-im/pkg/mq"
  7. "egg-im/pkg/pb"
  8. "time"
  9. "github.com/go-redis/redis"
  10. "go.uber.org/zap"
  11. "google.golang.org/protobuf/proto"
  12. )
  13. // StartSubscribe 启动MQ消息处理逻辑
  14. func StartSubscribe() {
  15. pushRoomPriorityChannel := db.RedisCli.Subscribe(mq.PushRoomPriorityTopic).Channel()
  16. pushRoomChannel := db.RedisCli.Subscribe(mq.PushRoomTopic).Channel()
  17. for i := 0; i < config.PushRoomSubscribeNum; i++ {
  18. go handlePushRoomMsg(pushRoomPriorityChannel, pushRoomChannel)
  19. }
  20. pushAllChannel := db.RedisCli.Subscribe(mq.PushAllTopic).Channel()
  21. for i := 0; i < config.PushAllSubscribeNum; i++ {
  22. go handlePushAllMsg(pushAllChannel)
  23. }
  24. }
  25. func handlePushRoomMsg(priorityChannel, channel <-chan *redis.Message) {
  26. for {
  27. select {
  28. case msg := <-priorityChannel:
  29. handlePushRoom([]byte(msg.Payload))
  30. default:
  31. select {
  32. case msg := <-channel:
  33. handlePushRoom([]byte(msg.Payload))
  34. default:
  35. time.Sleep(100 * time.Millisecond)
  36. continue
  37. }
  38. }
  39. }
  40. }
  41. func handlePushAllMsg(channel <-chan *redis.Message) {
  42. for msg := range channel {
  43. handlePushAll([]byte(msg.Payload))
  44. }
  45. }
  46. func handlePushRoom(bytes []byte) {
  47. var msg pb.PushRoomMsg
  48. err := proto.Unmarshal(bytes, &msg)
  49. if err != nil {
  50. logger.Logger.Error("handlePushRoom error", zap.Error(err))
  51. return
  52. }
  53. PushRoom(msg.RoomId, msg.MessageSend)
  54. }
  55. func handlePushAll(bytes []byte) {
  56. var msg pb.PushAllMsg
  57. err := proto.Unmarshal(bytes, &msg)
  58. if err != nil {
  59. logger.Logger.Error("handlePushRoom error", zap.Error(err))
  60. return
  61. }
  62. PushAll(msg.MessageSend)
  63. }