pubsub.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 (
  16. "bytes"
  17. )
  18. // Subscribe represents a subscribe or unsubscribe notification.
  19. type Subscription struct {
  20. // Kind is "subscribe", "unsubscribe", "psubscribe" or "punsubscribe"
  21. Kind string
  22. // The channel that was changed.
  23. Channel string
  24. // The current number of subscriptions for connection.
  25. Count int
  26. }
  27. // Message represents a message notification.
  28. type Message struct {
  29. // The originating channel.
  30. Channel string
  31. // The message data.
  32. Data []byte
  33. }
  34. // PubSubConn wraps a Conn with convenience methods for subscribers.
  35. type PubSubConn struct {
  36. Conn Conn
  37. }
  38. // Close closes the connection.
  39. func (c PubSubConn) Close() error {
  40. return c.Conn.Close()
  41. }
  42. // Subscribe subscribes the connection to the specified channels.
  43. func (c PubSubConn) Subscribe(channel ...interface{}) error {
  44. c.Conn.Send("SUBSCRIBE", channel...)
  45. return c.Conn.Flush()
  46. }
  47. // PSubscribe subscribes the connection to the given patterns.
  48. func (c PubSubConn) PSubscribe(channel ...interface{}) error {
  49. c.Conn.Send("PSUBSCRIBE", channel...)
  50. return c.Conn.Flush()
  51. }
  52. // Unsubscribe unsubscribes the connection from the given channels, or from all
  53. // of them if none is given.
  54. func (c PubSubConn) Unsubscribe(channel ...interface{}) error {
  55. c.Conn.Send("UNSUBSCRIBE", channel...)
  56. return c.Conn.Flush()
  57. }
  58. // PUnsubscribe unsubscribes the connection from the given patterns, or from all
  59. // of them if none is given.
  60. func (c PubSubConn) PUnsubscribe(channel ...interface{}) error {
  61. c.Conn.Send("PUNSUBSCRIBE", channel...)
  62. return c.Conn.Flush()
  63. }
  64. var messageBytes = []byte("message")
  65. // Receive returns a pushed message as a Subscription, Message or error. The
  66. // return value is intended to be used directly in a type switch as illustrated
  67. // in the PubSubConn example.
  68. func (c PubSubConn) Receive() interface{} {
  69. multiBulk, err := MultiBulk(c.Conn.Receive())
  70. if err != nil {
  71. return err
  72. }
  73. var kind []byte
  74. var channel string
  75. multiBulk, err = Values(multiBulk, &kind, &channel)
  76. if err != nil {
  77. return err
  78. }
  79. if bytes.Equal(kind, messageBytes) {
  80. var data []byte
  81. if _, err := Values(multiBulk, &data); err != nil {
  82. return err
  83. }
  84. return Message{channel, data}
  85. }
  86. var count int
  87. if _, err := Values(multiBulk, &count); err != nil {
  88. return err
  89. }
  90. return Subscription{string(kind), channel, count}
  91. }