PUBLISH
PUBLISH channel message
将信息 message 发送到指定的频道 channel 。
可用版本:
>= 2.0.0
时间复杂度:
O(N+M),其中 N 是频道 channel 的订阅者数量,而 M 则是使用模式订阅(subscribed patterns)的客户端的数量。
返回值:
接收到信息 message 的订阅者数量。
例子
# 对没有订阅者的频道发送信息
redis> publish bad_channel "can any body hear me?"
(integer) 0
# 向有一个订阅者的频道发送信息
redis> publish msg "good morning"
(integer) 1
# 向有多个订阅者的频道发送信息
redis> publish chat_room "hello~ everyone"
(integer) 3
SUBSCRIBE
SUBSCRIBE channel [channel ...]
订阅给定的一个或多个频道的信息。
可用版本:
>= 2.0.0
时间复杂度:
O(N),其中 N 是订阅的频道的数量。
返回值:
接收到的信息(请参见下面的代码说明)。
例子
# 订阅 msg 和 chat_room 两个频道
# 1 - 6 行是执行 subscribe 之后的反馈信息
# 第 7 - 9 行才是接收到的第一条信息
# 第 10 - 12 行是第二条
redis> subscribe msg chat_room
Reading messages... (press Ctrl-C to quit)
1) "subscribe" # 返回值的类型:显示订阅成功
2) "msg" # 订阅的频道名字
3) (integer) 1 # 目前已订阅的频道数量
1) "subscribe"
2) "chat_room"
3) (integer) 2
1) "message" # 返回值的类型:信息
2) "msg" # 来源(从那个频道发送过来)
3) "hello moto" # 信息内容
1) "message"
2) "chat_room"
3) "testing...haha"
golang示例1
package main
import (
"log"
"github.com/garyburd/redigo/redis"
)
func main() {
server := "10.194.80.35:6379"
option := redis.DialPassword("123456")
c, err := redis.Dial("tcp", server, option)
if err != nil {
log.Println("connect server failed:", err)
return
}
defer c.Close()
c.Send("SUBSCRIBE", "TongTianDaDao")
c.Flush()
for {
reply, err := redis.Values(c.Receive())
if err != nil {
log.Println("Receive failed:", err)
}
log.Println("reply:", reply)
for i,v := range reply {
switch vv:= v.(type){
case int64:
log.Println(i,":", vv)
case []byte:
log.Println(i,":", string(vv))
case string:
log.Println(i,":", vv)
default:
log.Println("unknown:",v)
}
}
}
}
output:
2018/06/29 14:26:30 reply: [[115 117 98 115 99 114 105 98 101] [84 111 110 103 84 105 97 110 68 97 68 97 111] 1]
2018/06/29 14:26:30 0 : subscribe
2018/06/29 14:26:30 1 : TongTianDaDao
2018/06/29 14:26:30 2 : 1
2018/06/29 14:27:22 reply: [[109 101 115 115 97 103 101] [84 111 110 103 84 105 97 110 68 97 68 97 111] [111 110 101 32 114 111 97 100]]
2018/06/29 14:27:22 0 : message
2018/06/29 14:27:22 1 : TongTianDaDao
2018/06/29 14:27:22 2 : one road
2018/06/29 14:28:04 reply: [[109 101 115 115 97 103 101] [84 111 110 103 84 105 97 110 68 97 68 97 111] [116 119 111 32 114 111 97 100 115]]
2018/06/29 14:28:04 0 : message
2018/06/29 14:28:04 1 : TongTianDaDao
2018/06/29 14:28:04 2 : two roads
golang示例2
使用库包裹函数
package main
import (
"log"
"github.com/garyburd/redigo/redis"
)
func main() {
server := "10.194.80.35:6379"
option := redis.DialPassword("123456")
c, err := redis.Dial("tcp", server, option)
if err != nil {
log.Println("connect server failed:", err)
return
}
defer c.Close()
psc := redis.PubSubConn{Conn:c}
psc.Subscribe("TongTianDaDao")
for {
switch v := psc.Receive().(type) {
case redis.Message:
log.Printf("%s: message: %s
", v.Channel, v.Data)
case redis.Subscription:
log.Printf("%s: %s %d
", v.Channel, v.Kind, v.Count)
case error:
log.Println("Receivd failed:", v)
}
}
}
output:
2018/06/29 14:37:09 TongTianDaDao: subscribe 1
2018/06/29 14:37:18 TongTianDaDao: message: one road
2018/06/29 14:37:20 TongTianDaDao: message: two roads
参考
https://github.com/gomodule/redigo