Class: Async::Slack::RealTime

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

Instance Method Summary collapse

Methods inherited from Representation

#initialize

Constructor Details

This class inherits a constructor from Async::Slack::Representation

Instance Method Details

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



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

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

#start(connection, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/async/slack/real_time.rb', line 44

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