Class: Slack::RtmApi2::Client
- Inherits:
-
Object
- Object
- Slack::RtmApi2::Client
- Defined in:
- lib/slack/rtmapi2/client.rb
Constant Summary collapse
- VALID =
[:open, :message, :error, :close]
Instance Attribute Summary collapse
-
#stop ⇒ Object
Returns the value of attribute stop.
Instance Method Summary collapse
- #connected? ⇒ Boolean
-
#init ⇒ Object
This init has been delayed because the SSL handshake is a blocking and expensive call.
-
#initialize(conf = {}) ⇒ Client
constructor
A new instance of Client.
-
#inner_loop ⇒ Object
All the polling work is done here.
-
#main_loop ⇒ Object
A dumb simple main loop.
- #on(type, &block) ⇒ Object
- #read_socket ⇒ Object
- #send(data) ⇒ Object
Constructor Details
#initialize(conf = {}) ⇒ Client
Returns a new instance of Client.
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/slack/rtmapi2/client.rb', line 12 def initialize(conf = {}) @websocket_url = conf.fetch(:websocket_url) @url = init_url @websocket_url @socket = conf[:socket] @driver = conf[:websocket_driver] @msg_queue = conf[:msg_queue] || [] @has_been_init = false @stop = false @reading = false @callbacks = {} end |
Instance Attribute Details
#stop ⇒ Object
Returns the value of attribute stop.
10 11 12 |
# File 'lib/slack/rtmapi2/client.rb', line 10 def stop @stop end |
Instance Method Details
#connected? ⇒ Boolean
85 86 87 |
# File 'lib/slack/rtmapi2/client.rb', line 85 def connected? @connected || false end |
#init ⇒ Object
This init has been delayed because the SSL handshake is a blocking and expensive call
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/slack/rtmapi2/client.rb', line 40 def init return if @has_been_init @socket = init_socket(@socket) @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 = JSON.parse event.data unless @callbacks[:message].nil? @callbacks[:message].call data end end @driver.on :close do |event| unless @callbacks[:close].nil? @callbacks[:close].call event.code, event.reason end end @driver.start @has_been_init = true end |
#inner_loop ⇒ Object
All the polling work is done here
103 104 105 106 107 108 109 |
# File 'lib/slack/rtmapi2/client.rb', line 103 def inner_loop return if @stop read_socket unless @reading @msg_queue.each { |msg| @driver.text msg } @msg_queue.clear sleep 0.1 end |
#main_loop ⇒ Object
A dumb simple main loop.
112 113 114 115 116 117 |
# File 'lib/slack/rtmapi2/client.rb', line 112 def main_loop init loop do inner_loop end end |
#on(type, &block) ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/slack/rtmapi2/client.rb', line 25 def on(type, &block) unless VALID.include? type raise ArgumentError.new "Client#on accept one of #{VALID.inspect}" end @callbacks[type] = block end |
#read_socket ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/slack/rtmapi2/client.rb', line 89 def read_socket @reading = true tries = 2 begin data = @socket.read_nonblock 4096 rescue OpenSSL::SSL::SSLErrorWaitReadable tries -= 1 retry if tries > 0 end @driver.parse data unless data.nil? || data.empty? @reading = false end |
#send(data) ⇒ Object
33 34 35 36 |
# File 'lib/slack/rtmapi2/client.rb', line 33 def send(data) data[:id] ||= SecureRandom.random_number 9_999_999 @msg_queue << data.to_json end |