actions.go 914 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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. ClientConnected ActionType = "CLIENT_CONNECTED"
  10. ClientDisconnected ActionType = "CLIENT_DISCONNECTED"
  11. )
  12. type Action struct {
  13. Type ActionType `json:"type"`
  14. FromClient *string `json:"fromClient"`
  15. Payload interface{} `json:"payload"`
  16. }
  17. func broadcastAction(thisPodClients *map[string]*Client, action *Action) []error {
  18. var errors []error
  19. for _, client := range(*thisPodClients) {
  20. if err := client.conn.WriteJSON(action); err != nil {
  21. errors = append(errors, err)
  22. }
  23. }
  24. return errors
  25. }
  26. func publishAction(rdb *redis.Client, action *Action) error {
  27. payload, err := json.Marshal(action)
  28. if err != nil {
  29. return err
  30. }
  31. if _, err := rdb.Publish(TOPIC_BROADCAST, payload).Result(); err != nil {
  32. return err
  33. }
  34. return nil
  35. }