actions.go 864 B

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