Class: Slack::RealTime::Client
- Inherits:
-
Object
- Object
- Slack::RealTime::Client
- Defined in:
- lib/slack/realtime/client.rb
Instance Method Summary collapse
-
#initialize(url) ⇒ Client
constructor
A new instance of Client.
- #on(type, &block) ⇒ Object
- #start ⇒ Object
Constructor Details
#initialize(url) ⇒ Client
Returns a new instance of Client.
7 8 9 10 |
# File 'lib/slack/realtime/client.rb', line 7 def initialize(url) @url = url @callbacks ||= {} end |
Instance Method Details
#on(type, &block) ⇒ Object
12 13 14 15 |
# File 'lib/slack/realtime/client.rb', line 12 def on(type, &block) @callbacks[type] ||= [] @callbacks[type] << block end |
#start ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/slack/realtime/client.rb', line 17 def start EM.run do ws = Faye::WebSocket::Client.new(@url, nil, ping: 30) ws.on :open do |event| end ws.on :message do |event| data = JSON.parse(event.data) if !data["type"].nil? && !@callbacks[data["type"].to_sym].nil? @callbacks[data["type"].to_sym].each do |c| c.call data end end end ws.on :close do |event| @callbacks[:close].each { |c| c.call } unless @callbacks[:close].nil? EM.stop end end end |