【问题标题】:How to use UNSUBSCRIBE command in Redis 2.6.11如何在 Redis 2.6.11 中使用 UNSUBSCRIBE 命令
【发布时间】:2013-03-16 01:05:41
【问题描述】:

向特定频道发布消息。

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

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

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

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

【问题讨论】:

    标签: redis


    【解决方案1】:

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

    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:

    r.unsubscribe if d == 'leave'
    

    【讨论】:

      猜你喜欢
      • 2013-11-23
      • 2012-05-14
      • 2015-09-26
      • 2022-01-08
      • 1970-01-01
      • 1970-01-01
      • 2021-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多