|event| p [:open] ws.send('Hello, world!') end ws.on :message do |event| p [:message, event.data] end ws.on :close do |event| p [:close, event.code, event.reason] ws = nil end }
URI.parse(ENV["REDISCLOUD_URL"]) @redis = Redis.new(host: uri.host, port: uri.port, password: uri.password) Thread.new do redis_sub = Redis.new(host: uri.host, port: uri.port, password: uri.password) redis_sub.subscribe(CHANNEL) do |on| on.message do |channel, msg| @clients.each {|ws| ws.send(msg) } end end end end
URI.parse(ENV["REDISCLOUD_URL"]) @redis = Redis.new(host: uri.host, port: uri.port, password: uri.password) Thread.new do redis_sub = Redis.new(host: uri.host, port: uri.port, password: uri.password) redis_sub.subscribe(CHANNEL) do |on| on.message do |channel, msg| @clients.each {|ws| ws.send(msg) } end end end end
:open do |event| p [:open, ws.object_id] @clients << ws end ws.on :message do |event| p [:message, event.data] @redis.publish(CHANNEL, sanitize(event.data)) end ws.on :close do |event| p [:close, ws.object_id, event.code, event.reason] @clients.delete(ws) ws = nil end # Return async Rack response ws.rack_response
:open do |event| p [:open, ws.object_id] @clients << ws end ws.on :message do |event| p [:message, event.data] @redis.publish(CHANNEL, sanitize(event.data)) end ws.on :close do |event| p [:close, ws.object_id, event.code, event.reason] @clients.delete(ws) ws = nil end # Return async Rack response ws.rack_response
:open do |event| p [:open, ws.object_id] @clients << ws end ws.on :message do |event| p [:message, event.data] @redis.publish(CHANNEL, sanitize(event.data)) end ws.on :close do |event| p [:close, ws.object_id, event.code, event.reason] @clients.delete(ws) ws = nil end # Return async Rack response ws.rack_response
:open do |event| p [:open, ws.object_id] @clients << ws end ws.on :message do |event| p [:message, event.data] @redis.publish(CHANNEL, sanitize(event.data)) end ws.on :close do |event| p [:close, ws.object_id, event.code, event.reason] @clients.delete(ws) ws = nil end # Return async Rack response ws.rack_response
:open do |event| p [:open, ws.object_id] @clients << ws end ws.on :message do |event| p [:message, event.data] @redis.publish(CHANNEL, sanitize(event.data)) end ws.on :close do |event| p [:close, ws.object_id, event.code, event.reason] @clients.delete(ws) ws = nil end # Return async Rack response ws.rack_response
:open do |event| p [:open, ws.object_id] @clients << ws end ws.on :message do |event| p [:message, event.data] @redis.publish(CHANNEL, sanitize(event.data)) end ws.on :close do |event| p [:close, ws.object_id, event.code, event.reason] @clients.delete(ws) ws = nil end # Return async Rack response ws.rack_response
r = Redis.new_redis r.subscribe channel do |on| on.message do |c, msg| websockets.each {|ws| ws.write msg} throw :empty if websockets.length == 0 end end end @@subscribed = false end
r = Redis.new_redis r.subscribe channel do |on| on.message do |c, msg| websockets.each {|ws| ws.write msg} throw :empty if websockets.length == 0 end end end @@subscribed = false end
%s\ndata: %s\n\n" def sse_event event_name, data data = data.to_json if Hash === data SSE_EVENT_TEMPLATE % [event_name.to_s, data] end def sse_message data data = data.to_json if Hash === data SSE_DATA_TEMPLATE % data end
r = Redis.new_redis r.subscribe channel do |on| on.message do |c, msg| sses.each {|es| es.write sse_message(msg)} throw :empty if sses.length == 0 end end end @@subscribed = false end
'/' do content_type :json raise RequestError.new foo: 'msg' end post '/' do halt 404 if params[:nope] end after do # runs after every route handler # unless RequestError is raised end
'/:foo/bar' do @foo = params[:foo] erb :index end get '/(avatar|user)/(?<device_id>.*).jpg', type: :regexp do @img_name = params[:device_id] erb :avatar end end