At this point, from another client we issue a PUBLISH operation against the channel named second:
同时,其他的客户端发布second通道:
> PUBLISH second Hellohis is what the first client receives:
第一个客户端将收到:
*3 $7 message $6 second $5 HelloNow 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:
匹配模式的发送消息类型格式有点不同:
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.