You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

145 lines
3.5 KiB

  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. package redis
  15. import "errors"
  16. // Subscription represents a subscribe or unsubscribe notification.
  17. type Subscription struct {
  18. // Kind is "subscribe", "unsubscribe", "psubscribe" or "punsubscribe"
  19. Kind string
  20. // The channel that was changed.
  21. Channel string
  22. // The current number of subscriptions for connection.
  23. Count int
  24. }
  25. // Message represents a message notification.
  26. type Message struct {
  27. // The originating channel.
  28. Channel string
  29. // The message data.
  30. Data []byte
  31. }
  32. // PMessage represents a pmessage notification.
  33. type PMessage struct {
  34. // The matched pattern.
  35. Pattern string
  36. // The originating channel.
  37. Channel string
  38. // The message data.
  39. Data []byte
  40. }
  41. // Pong represents a pubsub pong notification.
  42. type Pong struct {
  43. Data string
  44. }
  45. // PubSubConn wraps a Conn with convenience methods for subscribers.
  46. type PubSubConn struct {
  47. Conn Conn
  48. }
  49. // Close closes the connection.
  50. func (c PubSubConn) Close() error {
  51. return c.Conn.Close()
  52. }
  53. // Subscribe subscribes the connection to the specified channels.
  54. func (c PubSubConn) Subscribe(channel ...interface{}) error {
  55. c.Conn.Send("SUBSCRIBE", channel...)
  56. return c.Conn.Flush()
  57. }
  58. // PSubscribe subscribes the connection to the given patterns.
  59. func (c PubSubConn) PSubscribe(channel ...interface{}) error {
  60. c.Conn.Send("PSUBSCRIBE", channel...)
  61. return c.Conn.Flush()
  62. }
  63. // Unsubscribe unsubscribes the connection from the given channels, or from all
  64. // of them if none is given.
  65. func (c PubSubConn) Unsubscribe(channel ...interface{}) error {
  66. c.Conn.Send("UNSUBSCRIBE", channel...)
  67. return c.Conn.Flush()
  68. }
  69. // PUnsubscribe unsubscribes the connection from the given patterns, or from all
  70. // of them if none is given.
  71. func (c PubSubConn) PUnsubscribe(channel ...interface{}) error {
  72. c.Conn.Send("PUNSUBSCRIBE", channel...)
  73. return c.Conn.Flush()
  74. }
  75. // Ping sends a PING to the server with the specified data.
  76. func (c PubSubConn) Ping(data string) error {
  77. c.Conn.Send("PING", data)
  78. return c.Conn.Flush()
  79. }
  80. // Receive returns a pushed message as a Subscription, Message, PMessage, Pong
  81. // or error. The return value is intended to be used directly in a type switch
  82. // as illustrated in the PubSubConn example.
  83. func (c PubSubConn) Receive() interface{} {
  84. reply, err := Values(c.Conn.Receive())
  85. if err != nil {
  86. return err
  87. }
  88. var kind string
  89. reply, err = Scan(reply, &kind)
  90. if err != nil {
  91. return err
  92. }
  93. switch kind {
  94. case "message":
  95. var m Message
  96. if _, err := Scan(reply, &m.Channel, &m.Data); err != nil {
  97. return err
  98. }
  99. return m
  100. case "pmessage":
  101. var pm PMessage
  102. if _, err := Scan(reply, &pm.Pattern, &pm.Channel, &pm.Data); err != nil {
  103. return err
  104. }
  105. return pm
  106. case "subscribe", "psubscribe", "unsubscribe", "punsubscribe":
  107. s := Subscription{Kind: kind}
  108. if _, err := Scan(reply, &s.Channel, &s.Count); err != nil {
  109. return err
  110. }
  111. return s
  112. case "pong":
  113. var p Pong
  114. if _, err := Scan(reply, &p.Data); err != nil {
  115. return err
  116. }
  117. return p
  118. }
  119. return errors.New("redigo: unknown pubsub notification")
  120. }