Skip to content

Commit

Permalink
Fixed RTM example for async.
Browse files Browse the repository at this point in the history
  • Loading branch information
dblock committed Aug 27, 2018
1 parent 58c2980 commit db4851f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 39 deletions.
8 changes: 1 addition & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-08-25 18:15:23 +0200 using RuboCop version 0.58.2.
# on 2018-08-27 13:21:41 +0200 using RuboCop version 0.58.2.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 1
# Configuration parameters: AllowSafeAssignment.
Lint/AssignmentInCondition:
Exclude:
- 'lib/slack/real_time/concurrency/async.rb'

# Offense count: 4
Lint/HandleExceptions:
Exclude:
Expand Down
44 changes: 23 additions & 21 deletions examples/hi_real_time_async_async/hi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,35 @@

raise 'Missing ENV[SLACK_API_TOKENS]!' unless ENV.key?('SLACK_API_TOKENS')

Async::Reactor.run do
$stdout.sync = true
logger = Logger.new($stdout)
logger.level = Logger::DEBUG
$stdout.sync = true
logger = Logger.new($stdout)
logger.level = Logger::DEBUG

ENV['SLACK_API_TOKENS'].split.each do |token|
logger.info "Starting #{token[0..12]} ..."
threads = []

client = Slack::RealTime::Client.new(token: token)
ENV['SLACK_API_TOKENS'].split.each do |token|
logger.info "Starting #{token[0..12]} ..."

client.on :hello do
logger.info "Successfully connected, welcome '#{client.self.name}' to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
end
client = Slack::RealTime::Client.new(token: token)

client.on :message do |data|
logger.info data
client.on :hello do
logger.info "Successfully connected, welcome '#{client.self.name}' to the '#{client.team.name}' team at https://#{client.team.domain}.slack.com."
end

client.typing channel: data.channel
client.on :message do |data|
logger.info data

case data.text
when /hi/ then
client.message channel: data.channel, text: "Hi <@#{data.user}>!"
else
client.message channel: data.channel, text: "Sorry <@#{data.user}>, what?"
end
end
client.typing channel: data.channel

client.start_async
case data.text
when /hi/ then
client.message channel: data.channel, text: "Hi <@#{data.user}>!"
else
client.message channel: data.channel, text: "Sorry <@#{data.user}>, what?"
end
end

threads << client.start_async
end

threads.each(&:join)
15 changes: 5 additions & 10 deletions lib/slack/real_time/concurrency/async.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,15 @@ class Socket < Slack::RealTime::Socket

def start_async(client)
Thread.new do
$stderr.puts "start_async: @driver = #{@driver}"
::Async::Reactor.run do
client.run_loop
end
end
end

def connect!
::Async::Reactor.run do
super

run_loop
end
super
run_loop
end

def close
Expand All @@ -43,7 +39,7 @@ def close
end

def run_loop
while @driver and event = @driver.next_event
while @driver && @driver.next_event
# $stderr.puts event.inspect
end
end
Expand All @@ -64,10 +60,9 @@ def build_tcp_options
def build_endpoint
endpoint = ::Async::IO::Endpoint.tcp(addr, port, build_tcp_options)
endpoint = ::Async::IO::SSLEndpoint.new(endpoint, build_ssl_context) if secure?

return endpoint
endpoint
end

def connect
@driver = Client.new(build_endpoint.connect, url)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..'))

require 'rubygems'
require 'rspec'
Bundler.require

require 'slack-ruby-client'

Expand Down

0 comments on commit db4851f

Please sign in to comment.