actions.go 985 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package server
  2. import (
  3. "encoding/json"
  4. "github.com/felamaslen/gmus-backend/pkg/logger"
  5. "github.com/go-redis/redis/v7"
  6. )
  7. type ActionType string
  8. const (
  9. StateSet ActionType = "STATE_SET"
  10. ClientListUpdated = "CLIENT_LIST_UPDATED"
  11. )
  12. type Action struct {
  13. Type ActionType `json:"type"`
  14. FromClient *string `json:"fromClient"`
  15. Payload interface{} `json:"payload"`
  16. }
  17. func broadcastAction(l *logger.Logger, thisPodClients *map[string]*Client, action *Action) []error {
  18. var errors []error
  19. for _, client := range *thisPodClients {
  20. l.Debug("[->Client] %s (%s)\n", action.Type, client.name)
  21. if err := client.send(action); err != nil {
  22. errors = append(errors, err)
  23. }
  24. }
  25. return errors
  26. }
  27. func publishAction(rdb *redis.Client, action *Action) error {
  28. pubsubPayload, err := json.Marshal(action)
  29. if err != nil {
  30. return err
  31. }
  32. if _, err := rdb.Publish(TOPIC_BROADCAST, pubsubPayload).Result(); err != nil {
  33. return err
  34. }
  35. return nil
  36. }