forked from redis/redis-rb
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:ezmobius/redis-rb
- Loading branch information
Showing
1 changed file
with
27 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,31 @@ | ||
require 'rubygems' | ||
require 'redis' | ||
|
||
puts "To play with this example use redis-cli from another terminal, like this:" | ||
puts " ./redis-cli publish one hello" | ||
puts "Finally force the example to exit sending the 'exit' message with" | ||
puts " ./redis-cli publish two exit" | ||
puts "" | ||
|
||
@redis = Redis.new(:timeout => 0) | ||
|
||
@redis.subscribe('one','two') do |on| | ||
on.subscribe {|klass, num_subs| puts "Subscribed to #{klass} (#{num_subs} subscriptions)" } | ||
on.message do |klass, msg| | ||
puts "#{klass} received: #{msg}" | ||
if msg == 'exit' | ||
@redis.unsubscribe | ||
end | ||
end | ||
on.unsubscribe {|klass, num_subs| puts "Unsubscribed to #{klass} (#{num_subs} subscriptions)" } | ||
end | ||
require "redis" | ||
|
||
puts <<-EOS | ||
To play with this example use redis-cli from another terminal, like this: | ||
$ redis-cli publish one hello | ||
Finally force the example to exit sending the 'exit' message with: | ||
$ redis-cli publish two exit | ||
EOS | ||
|
||
redis = Redis.connect | ||
|
||
trap(:INT) { puts; exit } | ||
|
||
redis.subscribe(:one, :two) do |on| | ||
on.subscribe do |channel, subscriptions| | ||
puts "Subscribed to ##{channel} (#{subscriptions} subscriptions)" | ||
end | ||
|
||
on.message do |channel, message| | ||
puts "##{klass}: #{message}" | ||
redis.unsubscribe if message == "exit" | ||
end | ||
|
||
on.unsubscribe do |channel, subscriptions| | ||
puts "Unsubscribed from ##{klass} (#{subscriptions} subscriptions)" | ||
end | ||
end |