pubsub.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. //
  77. // The connection must be subscribed to at least one channel or pattern when
  78. // calling this method.
  79. func (c PubSubConn) Ping(data string) error {
  80. c.Conn.Send("PING", data)
  81. return c.Conn.Flush()
  82. }
  83. // Receive returns a pushed message as a Subscription, Message, PMessage, Pong
  84. // or error. The return value is intended to be used directly in a type switch
  85. // as illustrated in the PubSubConn example.
  86. func (c PubSubConn) Receive() interface{} {
  87. reply, err := Values(c.Conn.Receive())
  88. if err != nil {
  89. return err
  90. }
  91. var kind string
  92. reply, err = Scan(reply, &kind)
  93. if err != nil {
  94. return err
  95. }
  96. switch kind {
  97. case "message":
  98. var m Message
  99. if _, err := Scan(reply, &m.Channel, &m.Data); err != nil {
  100. return err
  101. }
  102. return m
  103. case "pmessage":
  104. var pm PMessage
  105. if _, err := Scan(reply, &pm.Pattern, &pm.Channel, &pm.Data); err != nil {
  106. return err
  107. }
  108. return pm
  109. case "subscribe", "psubscribe", "unsubscribe", "punsubscribe":
  110. s := Subscription{Kind: kind}
  111. if _, err := Scan(reply, &s.Channel, &s.Count); err != nil {
  112. return err
  113. }
  114. return s
  115. case "pong":
  116. var p Pong
  117. if _, err := Scan(reply, &p.Data); err != nil {
  118. return err
  119. }
  120. return p
  121. }
  122. return errors.New("redigo: unknown pubsub notification")
  123. }