如何在 Redis 2.6.11 中使用 UNSUBSCRIBE 命令 [英] How to use UNSUBSCRIBE command in Redis 2.6.11

查看:51
本文介绍了如何在 Redis 2.6.11 中使用 UNSUBSCRIBE 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

向特定频道发布消息.

redis 127.0.0.1:6379> PUBLISH channel message
(integer) 0

我使用另一个 Redis 客户端订阅了频道.

Using another Redis client I subscribed the channel .

redis 127.0.0.1:6379> SUBSCRIBE channel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel"
3) (integer) 1

在 Redis 客户端中,我收到了所有已发布的消息.现在我想取消订阅已订阅的频道.但是我无法在 Redis 客户端中输入 unsubscribe .当我使用 Ctrl+c 时,Redis 客户端会退出.Redis客户端如何编写取消订阅命令?

In the Redis client I got all the Published messages. Now I want to unsubscribe from the subscribed channel. But I cant type unsubscribe in the Redis client. When I use Ctrl+c full Redis client quits. How to write the Unsubscribe command in Redis Client?

推荐答案

我不认为您可以在客户端发出取消订阅,因为客户端被阻止.我写了一个 ruby​​ 脚本来展示如何使用取消订阅.

I dont think you can issue unsubscribe in the client cause the client was blocked. I wrote a ruby script to show how to use unsubscribe.

require 'redis'
r = Redis.new
r.subscribe 'first' do |on|
  on.message do |e, d|
    puts e
    puts d
    r.unsubscribe
  end
end
puts "script was blocked?"

如果您删除 r.unsubscribe,脚本将被阻止.您可以添加 if 子句来检查何时取消订阅 client.ex:

If you remove r.unsubscribe, the script will be blocked. And you can add if clause to check when to unsubscribe client.ex:

r.unsubscribe if d == 'leave'

这篇关于如何在 Redis 2.6.11 中使用 UNSUBSCRIBE 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆