class Slack::RealTimeClient
Attributes
connection[R]
session[R]
Public Class Methods
new(session)
click to toggle source
# File lib/laziness/real_time_client.rb, line 8 def initialize(session) @session = session @events = Registry.new end
Public Instance Methods
broadcast(channel, message, options={})
click to toggle source
# File lib/laziness/real_time_client.rb, line 13 def broadcast(channel, message, options={}) attributes = { channel: channel, text: message }.merge(options) connection.send Message.generate(attributes).to_json end
off(event=nil, handler=nil, func=:update, &blk)
click to toggle source
# File lib/laziness/real_time_client.rb, line 39 def off(event=nil, handler=nil, func=:update, &blk) @events.unregister event, handler, func, &blk end
on(event=nil, handler=nil, func=:update, &blk)
click to toggle source
# File lib/laziness/real_time_client.rb, line 34 def on(event=nil, handler=nil, func=:update, &blk) @events.register event, handler, func, &blk self end
run(queue=nil, options={})
click to toggle source
# File lib/laziness/real_time_client.rb, line 18 def run(queue=nil, options={}) EM.run do connect(options) connection.on(:open) { |event| EM.defer { @events.notify(:open, event) }} connection.on(:message) { |event| send_message(event) } connection.on(:close) do |event| EM.defer { @events.notify(:close, event) } shutdown end connection.on(:error) { |event| EM.defer { @events.notify(:error, event) }} queue << connection if queue end end
shutdown()
click to toggle source
# File lib/laziness/real_time_client.rb, line 43 def shutdown connection.close if connection EM.stop if EM.reactor_running? @events.clear end
Private Instance Methods
connect(options={})
click to toggle source
# File lib/laziness/real_time_client.rb, line 53 def connect(options={}) @connection ||= Faye::WebSocket::Client.new(session.url, nil, default_options.merge(options)) end
default_options()
click to toggle source
# File lib/laziness/real_time_client.rb, line 58 def default_options { ping: 10 } end
send_message(event)
click to toggle source
# File lib/laziness/real_time_client.rb, line 62 def send_message(event) message = Message.parse event.data EM.defer { @events.notify(message.type, message) } end