testsub.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gomodule/redigo/redis"
  5. "reflect"
  6. //"github.com/vmihailenco/msgpack/v5"
  7. )
  8. // dial wraps DialDefaultServer() with a more suitable function name for examples.
  9. func dial() (redis.Conn, error) {
  10. c, err := redis.Dial("tcp", "127.0.0.1:11001")
  11. if err != nil {
  12. return nil ,err
  13. }
  14. return c, nil
  15. }
  16. func main() {
  17. c, err := dial()
  18. if err != nil {
  19. fmt.Println(err)
  20. return
  21. }
  22. defer c.Close()
  23. //c.Send("SUBSCRIBE", "example")
  24. c.Send("SUBSCRIBE", "CLASS_DATA_CHANGE:/matrix/devops/@matrix")
  25. c.Flush()
  26. count := 0
  27. for {
  28. count++
  29. reply, err := c.Receive()
  30. if err != nil {
  31. fmt.Printf("err: %v", err)
  32. }else{
  33. switch data := reply.(type) {
  34. case string : fmt.Printf("reply: %s", data)
  35. case int : fmt.Printf("reply: %d", data)
  36. case []interface {} :
  37. fmt.Printf("reply: ")
  38. for _, item := range data {
  39. switch v := item.(type) {
  40. case string : fmt.Printf(" %s ", v)
  41. case []uint8: fmt.Printf(" %s ", string(v))
  42. case int64 : fmt.Printf(" %d ", v)
  43. case int : fmt.Printf(" %d ", v)
  44. default : fmt.Printf("type: %v", reflect.TypeOf(item))
  45. }
  46. }
  47. fmt.Printf("\n")
  48. default : fmt.Printf("type: %v", reflect.TypeOf(reply))
  49. }
  50. }
  51. if count == 2 {
  52. break
  53. }
  54. // process pushed message
  55. }
  56. //c.Send("UNSUBSCRIBE", "CLASS_DATA_CHANGE:/matrix/devops/change@matrix")
  57. c.Send("quit")
  58. c.Flush()
  59. }