Created
April 18, 2013 19:51
-
-
Save ohcibi/5415709 to your computer and use it in GitHub Desktop.
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
class My::Channel < EM::Channel | |
attr_accessor :watcher, :logger | |
def self.create! | |
channel = new | |
channel.logger = Logger.new 'sinatra.log' | |
channel.watcher | |
channel | |
end | |
def watcher | |
@watcher ||= fork do | |
redis = Redis.new | |
redis.subscribe("foobar") do |on| | |
on.message do |channel, message| | |
self.logger.info message | |
self.push "asdfasf" | |
My::FakeWebSocket.settings.sockets.each do |s| | |
self.logger.info "socket notified" | |
s.send message | |
end | |
end | |
end | |
end | |
end | |
end |
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
set :channel, My::Channel.create! | |
get '/' do | |
request.websocket do |ws| | |
ws.onopen do | |
settings.sockets << ws | |
subscriber_id = settings.channel.subscribe do |json| | |
LOGGER.info "the channel spoke" | |
LOGGER.info json | |
ws.send json | |
end | |
settings.subscriptions << subscriber_id | |
end | |
ws.onclose do | |
warn("closing websocket") | |
settings.sockets.delete ws | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment