channel_client.go 1008 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package redis
  2. import (
  3. "time"
  4. "bet24.com/log"
  5. "github.com/gomodule/redigo/redis"
  6. )
  7. const channel_name = "rbwar_channel"
  8. func Subscribe(onCommand func(data string)) {
  9. conn := RedisClient.Get()
  10. if conn == nil {
  11. log.Release("Subscribe error conn == nil")
  12. time.AfterFunc(time.Minute, func() {
  13. Subscribe(onCommand)
  14. })
  15. return
  16. }
  17. psc := redis.PubSubConn{Conn: conn}
  18. psc.Subscribe(channel_name)
  19. for {
  20. switch v := psc.Receive().(type) {
  21. case redis.Message:
  22. //log.Debug("%s: message: %s\n", v.Channel, v.Data)
  23. onCommand(string(v.Data))
  24. case redis.Subscription:
  25. log.Release("%s: %s %d\n", v.Channel, v.Kind, v.Count)
  26. case error:
  27. log.Release("Subscribe error %v", v)
  28. time.AfterFunc(time.Minute, func() {
  29. Subscribe(onCommand)
  30. })
  31. return
  32. }
  33. }
  34. }
  35. func Publish(data string) {
  36. go func(d string) {
  37. conn := RedisClient.Get()
  38. if conn == nil {
  39. log.Release("Publish no pool connection")
  40. return
  41. }
  42. conn.Do("PUBLISH", channel_name, d)
  43. conn.Close()
  44. }(data)
  45. }