Pub/Sub发布订阅(二)

2015-07-24 06:57:32 · 作者: · 浏览: 7
:1 *3 $9 subscribe $6 second :2
At this point, from another client we issue a PUBLISH operation against the channel named second:
同时,其他的客户端发布second通道:
 
 
> PUBLISH second Hello
his is what the first client receives:
第一个客户端将收到:
 
 
*3 $7 message $6 second $5 Hello
Now the client unsubscribes itself from all the channels using the UNSUBSCRIBE command without additional arguments:
现在客户端使用UNSUBSCRIBE命令不带参数取消自己定义的所有通道:
UNSUBSCRIBE
*3
$11
unsubscribe
$6
second
:1
*3
$11
unsubscribe
$5
first
:0

?

Pattern-matching subscriptions 模式匹配订阅

The Redis Pub/Sub implementation supports pattern matching. Clients may subscribe to glob-style patterns in order to receive all the messages sent to channel names matching a given pattern.
Redis的 pub/sub的实现支持模式匹配。客户端可以使用全局样式订阅并接受所有和给出的模式相匹配的所有通道消息。 For instance: 例如:
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:
匹配模式的发送消息类型格式有点不同:
The type of the message is 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. 类型是pmessage:它是客户端使用PUBLISH命令发布的消息,匹配一个模式匹配订阅。第二个元素代表匹配,第三个元素通道名字的开始,最后一个元素是真正消息内容。 Similarly to SUBSCRIBE and UNSUBSCRIBE, PSUBSCRIBE and PUNSUBSCRIBE commands are acknowledged by the system sending a message of type psubscribe and punsubscribe using the same format as the subscribe andunsubscribe message format 与.SUBSCRIBE和UNSUBSCRIBE一样,PSUBSCRIBE和PUNSUBSCRUBE命令使用与subscribe 和unsubscribe发送相同的消息格式发送消息也是允许的。

Messages matching both a pattern and a channel subscription匹配订阅和一般订阅


A client may receive a single message multiple times if it's subscribed to multiple patterns matching a published message, or if it is subscribed to both patterns and channels matching the message. Like in the following example:
一个客户端如果订阅多匹配模式发布的消息,或者如果订阅了两个模式和通道匹配的消息,那么这个客户端可能会收到一个消息多次。就像下面这样:
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 messageand one of type pmessage.
在上面的例子,如果一个foo通道的消息被发送,这个客户端将会收到两个消息:一个类型是messageand 另一个类型是pmessage

The meaning of the subscription count with pattern matching订阅数量的意义

In subscribe, 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.

Programming example 实现实例

Pieter Noordhuis provided a great example using EventMachine and Redis to create a multi use