Skip to content

Instantly share code, notes, and snippets.

@ohcibi
Created April 18, 2013 19:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohcibi/5415709 to your computer and use it in GitHub Desktop.
Save ohcibi/5415709 to your computer and use it in GitHub Desktop.
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
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