class Slacks::Driver
Constants
- VALID
Attributes
callbacks[R]
driver[R]
socket[R]
stop[RW]
url[R]
Public Class Methods
new()
click to toggle source
# File lib/slacks/driver.rb, line 11 def initialize @has_been_init = false @stop = false @callbacks = {} end
Public Instance Methods
connect_to(url)
click to toggle source
This init has been delayed because the SSL handshake is a blocking and expensive call
# File lib/slacks/driver.rb, line 28 def connect_to(url) raise "Already been init" if @has_been_init url = URI(url) raise ArgumentError.new ":url must be a valid websocket secure url!" unless url.scheme == "wss" @socket = OpenSSL::SSL::SSLSocket.new(TCPSocket.new url.host, 443) socket.connect # costly and blocking ! internalWrapper = (Struct.new :url, :socket do def write(*args) self.socket.write(*args) end end).new url.to_s, socket # this, also, is costly and blocking @driver = WebSocket::Driver.client internalWrapper driver.on :open do @connected = true unless callbacks[:open].nil? callbacks[:open].call end end driver.on :error do |event| @connected = false unless callbacks[:error].nil? callbacks[:error].call event end end driver.on :message do |event| data = MultiJson.load event.data unless callbacks[:message].nil? callbacks[:message].call data end end driver.start @has_been_init = true end
connected?()
click to toggle source
# File lib/slacks/driver.rb, line 77 def connected? @connected || false end
inner_loop()
click to toggle source
All the polling work is done here
# File lib/slacks/driver.rb, line 82 def inner_loop return if @stop data = @socket.readpartial 4096 driver.parse data unless data.nil? or data.empty? end
main_loop()
click to toggle source
# File lib/slacks/driver.rb, line 89 def main_loop loop do inner_loop end end
on(type, &block)
click to toggle source
# File lib/slacks/driver.rb, line 18 def on(type, &block) unless VALID.include? type raise ArgumentError.new "Client#on accept one of #{VALID.inspect}" end callbacks[type] = block end
ping()
click to toggle source
# File lib/slacks/driver.rb, line 73 def ping @driver.ping end
write(*args)
click to toggle source
# File lib/slacks/driver.rb, line 37 def write(*args) self.socket.write(*args) end