練習:redis
命令爲: subscribe channelui
publish channel messagecode
➜ ~ redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> subscribe redisChannel Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "redisChannel" 3) (integer) 1 1) "message" 2) "redisChannel" 3) "Today is Friday" 1) "message" 2) "redisChannel" 3) "\xe5\x85\xb3\xe5\x85\xb3" 1) "message" 2) "redisChannel" 3) "gholly"
2.而後新建一個客戶端,往redisChannel通道發消息it
➜ ~ redis-cli -h 127.0.0.1 -p 6379 127.0.0.1:6379> publish redisChannel "Today is Friday" (integer) 1 127.0.0.1:6379> publish redisChannel "關關" (integer) 1 127.0.0.1:6379> publish redisChannel "gholly" (integer) 1 127.0.0.1:6379>
3.嘗試同時訂閱多個通道channel,class
127.0.0.1:6379> subscribe channel3 hh guanguan #表示訂閱了guanguan hh channel3這3個通道 Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "channel3" 3) (integer) 1 1) "subscribe" 2) "hh" 3) (integer) 2 1) "subscribe" 2) "guanguan" 3) (integer) 3 1) "message" 2) "channel3" 3) "I am channel3" 1) "message" 2) "hh" 3) "I am hh" 1) "message" 2) "guanguan" 3) "I am guanguan" -------------------- 127.0.0.1:6379> publish channel3 "I am channel3" #分別向這個3個通道發消息 (integer) 1 127.0.0.1:6379> publish hh "I am hh" (integer) 1 127.0.0.1:6379> publish guanguan "I am guanguan" (integer) 1 127.0.0.1:6379> publish gholly "I am gholly" (integer) 0 127.0.0.1:6379> 二者可通訊,可利用redis的訂閱發佈功能進行消息的即時推送功能
4.pubsub 查看全部訂閱狀況cli
127.0.0.1:6379> pubsub channels 1) "guanguan" 2) "hh" 3) "channel3"
5.psubscribe --訂閱給定模式的一個或多個channelchannel
127.0.0.1:6379> psubscribe gg.* --是指訂閱以gg.開頭的channel Reading messages... (press Ctrl-C to quit) 1) "psubscribe" 2) "gg.*" 3) (integer) 1 1) "pmessage" 2) "gg.*" 3) "gg." 4) "hhhhh" 1) "pmessage" 2) "gg.*" 3) "gg.hf" 4) "hhhhh" ---發佈 127.0.0.1:6379> publish gg "hhhhh" (integer) 0 127.0.0.1:6379> publish gg. "hhhhh" (integer) 1 127.0.0.1:6379> publish gg.hf "hhhhh" (integer) 1 127.0.0.1:6379>
6.unsubscribe 退訂通道im
127.0.0.1:6379> unsubscribe hh 1) "unsubscribe" 2) "hh" 3) (integer) 0
7.punsubscribe 退訂指定模式的通道通信
127.0.0.1:6379> punsubscribe gg.* 1) "punsubscribe" 2) "gg.*" 3) (integer) 0