actions.go 962 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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 client.name != *action.FromClient {
  21. if err := client.conn.WriteJSON(action); err != nil {
  22. errors = append(errors, err)
  23. }
  24. }
  25. }
  26. return errors
  27. }
  28. func publishAction(rdb *redis.Client, action *Action) error {
  29. payload, err := json.Marshal(action)
  30. if err != nil {
  31. return err
  32. }
  33. if _, err := rdb.Publish(TOPIC_BROADCAST, payload).Result(); err != nil {
  34. return err
  35. }
  36. return nil
  37. }