foo
and bar
the
client issues a SUBSCRIBE providing the names of the channels:SUBSCRIBE foo bar
Messages sent by other clients to these channels will be pushed by Redis to all the subscribed clients.The first element is the kind of message:
第一个元素表示消息的类型:
subscribe
: means that we successfully subscribed to the channel given as the second element
in the reply. The third argument represents the number of channels we are currently subscribed to.
订阅:意思是我们成功订阅到第二个元素代表的通道(消息类型)。第3个元素代表我们当前已经订阅的通道(消息类型)数量。
unsubscribe
: means that we successfully unsubscribed from the channel given as second element in the reply.
The third argument represents the number of channels we are currently subscribed to. When the last argument is zero, we are no longer subscribed to any channel, and the client can issue any kind of Redis command as we are outside the Pub/Sub state.message
: it is a message received as result of a PUBLISH command
issued by another client. The second element is the name of the originating channel, and the third argument is the actual message payload.
SUBSCRIBE first second
*3
$9
subscribe
$5
first
:1
*3
$9
subscribe
$6
second
:2
At this point, from another client we issue a PUBLISH operation against the channel named second
:
同时,其他的客户端发布second通道:
his is what the first client receives:> PUBLISH second Hello
第一个客户端将收到:
现在客户端使用UNSUBSCRIBE命令不带参数取消自己定义的所有通道:Now the client unsubscribes itself from all the channels using the UNSUBSCRIBE command without additional arguments:*3 $7 message $6 second $5 Hello
UNSUBSCRIBE
*3
$11
unsubscribe
$6
second
:1
*3
$11
unsubscribe
$5
first
:0
PSUBSCRIBE news.*
Will receive all the messages sent to the channel news.art.figurative
, news.music.jazz
,
etc. All the glob-style patterns are valid, so multiple wildcards are supported.news.art.figurative
, news.music.jazz等等。整个全局样式模式都是有效的,因此多个通配符也是支持的。
PUNSUBSCRIBE news.*
Will then unsubscribe the client from that pattern. No other subscriptions will be affected by this call.在该匹配模式中取消订阅。仅仅是已经订阅并且调用取消订阅的这个客户端受影响。
Messages
received as a result of pattern matching are sent in a different format:
匹配模式的发送消息类型格式有点不同:
pmessage
: it is a message received as result of a PUBLISH command
issued by another client, matching a pattern-matching subscription. The second element is the original pattern matched, the third element is the name of the originating channel, and the last element the actual message payload.psubscribe
and punsubscribe
using
the same format as the subscribe
andunsubscribe
message
formatsubscribe
和unsubscribe发送相同的消息格式发送消息也是允许的。SUBSCRIBE foo
PSUBSCRIBE f*
In the above example, if a message is sent to channel foo
, the client will receive two messages: one of type message
and
one of type pmessage
.message
and 另一个类型是pmessagesubscribe
, unsubscribe
, psubscribe
and punsubscribe
message
types, the last argument is the count of subscriptions still active. This number is actually the total number of channels and patterns the client is still subscribed to. So the client will exit the Pub/Sub state only when this count drops to zero as a result
of unsubscription from all the channels and patterns.在
subscribe
, unsubscribe
, psubscribe
和punsubscribe
的消息类型中,最后一个参数是变化的它表示订阅的数量。这个数量事实上是客户端当前订阅通道的总数。因此如果客户端取消所有的订阅这个值将下降到0.Because all the messages received contain the original subscription causing the message delivery (the channel in the case of message type, and the original pattern in the case of pmessage type) client libraries may bind the original subscription to callbacks (that can be anonymous functions, blocks, function pointers), using an hash table.
使用hash table 注册订阅通道与回调函数关联,当收到订阅通道消息时直接调用注册的函数处理。
原文地址:http://blog.csdn.net/guobangli/article/details/46509707