-
-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from dblock/async
Added support for async-websocket.
- Loading branch information
Showing
12 changed files
with
140 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
source 'http://rubygems.org' | ||
|
||
gem 'slack-ruby-client', path: '../..' | ||
|
||
gem 'async-websocket' | ||
gem 'foreman' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
console: bundle exec ruby hi.rb | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'slack-ruby-client' | ||
require 'async' | ||
|
||
raise 'Missing ENV[SLACK_API_TOKENS]!' unless ENV.key?('SLACK_API_TOKENS') | ||
|
||
$stdout.sync = true | ||
logger = Logger.new($stdout) | ||
logger.level = Logger::DEBUG | ||
|
||
threads = [] | ||
|
||
ENV['SLACK_API_TOKENS'].split.each do |token| | ||
logger.info "Starting #{token[0..12]} ..." | ||
|
||
client = Slack::RealTime::Client.new(token: token) | ||
|
||
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.on :message do |data| | ||
logger.info data | ||
|
||
client.typing channel: data.channel | ||
|
||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'async/websocket' | ||
|
||
module Slack | ||
module RealTime | ||
module Concurrency | ||
module Async | ||
class Client < ::Async::WebSocket::Client | ||
extend ::Forwardable | ||
def_delegators :@driver, :on, :text, :binary, :emit | ||
end | ||
|
||
class Socket < Slack::RealTime::Socket | ||
attr_reader :client | ||
|
||
def start_async(client) | ||
Thread.new do | ||
::Async::Reactor.run do | ||
client.run_loop | ||
end | ||
end | ||
end | ||
|
||
def connect! | ||
super | ||
run_loop | ||
end | ||
|
||
def close | ||
@closing = true | ||
@driver.close if @driver | ||
super | ||
end | ||
|
||
def run_loop | ||
@closing = false | ||
while @driver && @driver.next_event | ||
# $stderr.puts event.inspect | ||
end | ||
end | ||
|
||
protected | ||
|
||
def build_ssl_context | ||
OpenSSL::SSL::SSLContext.new(:TLSv1_2_client).tap do |ctx| | ||
ctx.set_params(verify_mode: OpenSSL::SSL::VERIFY_PEER) | ||
end | ||
end | ||
|
||
def build_endpoint | ||
endpoint = ::Async::IO::Endpoint.tcp(addr, port) | ||
endpoint = ::Async::IO::SSLEndpoint.new(endpoint, ssl_context: build_ssl_context) if secure? | ||
endpoint | ||
end | ||
|
||
def connect_socket | ||
build_endpoint.connect | ||
end | ||
|
||
def connect | ||
@socket = connect_socket | ||
@driver = Client.new(@socket, url) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,6 +74,7 @@ def stop_server | |
|
||
after do | ||
wait_for_server | ||
connection.join | ||
end | ||
|
||
context 'client connected' do | ||
|