Class: Slack::RealTimeClient
- Inherits:
-
Object
- Object
- Slack::RealTimeClient
- Defined in:
- lib/laziness/real_time_client.rb
Instance Attribute Summary collapse
-
#session ⇒ Object
readonly
Returns the value of attribute session.
Instance Method Summary collapse
- #broadcast(channel, message, options = {}) ⇒ Object
-
#initialize(session) ⇒ RealTimeClient
constructor
A new instance of RealTimeClient.
- #off(event = nil, handler = nil, func = :update, &blk) ⇒ Object
- #on(event = nil, handler = nil, func = :update, &blk) ⇒ Object
- #run(queue = nil, options = {}) ⇒ Object
- #shutdown ⇒ Object
Constructor Details
#initialize(session) ⇒ RealTimeClient
Returns a new instance of RealTimeClient.
8 9 10 11 |
# File 'lib/laziness/real_time_client.rb', line 8 def initialize(session) @session = session @events = Registry.new end |
Instance Attribute Details
#session ⇒ Object (readonly)
Returns the value of attribute session.
6 7 8 |
# File 'lib/laziness/real_time_client.rb', line 6 def session @session end |
Instance Method Details
#broadcast(channel, message, options = {}) ⇒ Object
13 14 15 16 |
# File 'lib/laziness/real_time_client.rb', line 13 def broadcast(channel, , ={}) attributes = { channel: channel, text: }.merge() connection.send Message.generate(attributes).to_json end |
#off(event = nil, handler = nil, func = :update, &blk) ⇒ Object
39 40 41 |
# 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) ⇒ Object
34 35 36 37 |
# 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 = {}) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/laziness/real_time_client.rb', line 18 def run(queue=nil, ={}) EM.run do connect() connection.on(:open) { |event| EM.defer { @events.notify(:open, event) }} connection.on(:message) { |event| (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 ⇒ Object
43 44 45 46 47 |
# File 'lib/laziness/real_time_client.rb', line 43 def shutdown connection.close if connection EM.stop if EM.reactor_running? @events.clear end |