Skip to content

Instantly share code, notes, and snippets.

@dblock
Created Jun 14, 2016
Embed
What would you like to do?
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