Class: Async::Slack::RealTime

Inherits:
Representation show all
Defined in:
lib/async/slack/real_time.rb

Instance Method Summary collapse

Instance Method Details

#connect(**options, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/async/slack/real_time.rb', line 32

def connect(**options, &block)
  response = self.post
  
  parameters = response.read
  
  if url = parameters[:url]
    endpoint = Async::HTTP::Endpoint.parse(url)
    
    Async::WebSocket::Client.connect(endpoint, **options) do |connection|
      self.start(connection, &block)
    end
  else
    raise ConnectionError, parameters
  end
end

#start(connection, &block) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/async/slack/real_time.rb', line 48

def start(connection, &block)
  id = 1
  
  pinger = Async do |task|
    while true
      task.sleep 60
      
      Async.logger.debug(self) {"Sending ping #{id}..."}
      connection.write({type: "ping", id: "pinger-#{id}"})
      connection.flush
      
      id += 1
    end
  end
  
  yield connection
ensure
  pinger.stop
end