Skip to content

Instantly share code, notes, and snippets.

@dblock
Created June 14, 2016 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dblock/1250a5497ff3c7af81c05964c459ff86 to your computer and use it in GitHub Desktop.
Save dblock/1250a5497ff3c7af81c05964c459ff86 to your computer and use it in GitHub Desktop.
Slack celluloid connection test.
require 'celluloid/current'
require 'celluloid/io'
require 'http'
require 'websocket/driver'
class Connection
include Celluloid::IO
extend Forwardable
def initialize(url)
@url = url
uri = URI.parse(url)
port = uri.port || (uri.scheme == "ws" ? 80 : 443)
@socket = Celluloid::IO::TCPSocket.new(uri.host, port)
@socket = SSLSocket.new(@socket, OpenSSL::SSL::SSLContext.new(:TLSv1_2_client))
@socket.connect
@client = ::WebSocket::Driver.client(self)
async.run
end
attr_reader :url
def run
@client.on('open') do |event|
puts "OPEN: #{event}"
end
@client.on('message') do |event|
puts "MESSAGE: #{event}"
end
@client.on('close') do |event|
puts "CLOSE: #{event}"
end
@client.start
loop do
begin
@client.parse(@socket.readpartial(1024))
rescue EOFError
puts "EOF"
@socket.close
break
end
end
end
def_delegators :@client, :text, :binary, :ping, :close, :protocol
def write(buffer)
@socket.write buffer
end
end
fail 'missing token' unless ENV['SLACK_API_TOKEN']
url = JSON.parse(HTTP.get("https://slack.com/api/rtm.start?token=#{ENV['SLACK_API_TOKEN']}"))['url']
puts "connecting to #{URI.parse(url).host} ..."
conn = Connection.new(url)
loop do
Thread.pass
end
puts "done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment