Class: Slack::RealTimeClient

Inherits:
Object
  • Object
show all
Defined in:
lib/laziness/real_time_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#sessionObject (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, 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) ⇒ 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, 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

#shutdownObject



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