class Slack::RealTime::Client
Attributes
callbacks[R]
web_client[RW]
Public Class Methods
config()
click to toggle source
# File lib/slack/real_time/client.rb, line 64 def config Config end
configure() { |config| ... }
click to toggle source
# File lib/slack/real_time/client.rb, line 60 def configure block_given? ? yield(config) : config end
new(options = {})
click to toggle source
# File lib/slack/real_time/client.rb, line 15 def initialize(options = {}) @callbacks = Hash.new { |h, k| h[k] = [] } Slack::RealTime::Config::ATTRIBUTES.each do |key| send("#{key}=", options[key] || Slack::RealTime.config.send(key)) end @token ||= Slack.config.token @web_client = Slack::Web::Client.new(token: token) end
Public Instance Methods
on(type, &block)
click to toggle source
# File lib/slack/real_time/client.rb, line 30 def on(type, &block) type = type.to_s callbacks[type] << block end
start!(&block)
click to toggle source
Start RealTime
client and block until it disconnects. @yieldparam [Websocket::Driver] driver
# File lib/slack/real_time/client.rb, line 37 def start!(&block) socket = build_socket socket.start_sync { run_loop(socket, &block) } end
start_async(&block)
click to toggle source
Start RealTime
client and return immediately. The RealTime::Client
will run in the background. @yieldparam [Websocket::Driver] driver
# File lib/slack/real_time/client.rb, line 45 def start_async(&block) socket = build_socket socket.start_async { run_loop(socket, &block) } end
started?()
click to toggle source
# File lib/slack/real_time/client.rb, line 55 def started? @socket && @socket.connected? end
stop!()
click to toggle source
# File lib/slack/real_time/client.rb, line 50 def stop! fail ClientNotStartedError unless started? @socket.disconnect! if @socket end
Protected Instance Methods
build_socket()
click to toggle source
@return [Slack::RealTime::Socket]
# File lib/slack/real_time/client.rb, line 72 def build_socket fail ClientAlreadyStartedError if started? @options = web_client.rtm_start socket_class.new(@options.fetch('url'), socket_options) end
callback(event, type)
click to toggle source
# File lib/slack/real_time/client.rb, line 130 def callback(event, type) callbacks = self.callbacks[type.to_s] return false unless callbacks callbacks.each do |c| c.call(event) end end
close(_event)
click to toggle source
# File lib/slack/real_time/client.rb, line 121 def close(_event) socket = @socket @socket = nil [socket, socket_class].each do |s| s.close if s.respond_to?(:close) end end
dispatch(event)
click to toggle source
# File lib/slack/real_time/client.rb, line 138 def dispatch(event) return false unless event.data data = JSON.parse(event.data) type = data['type'] return false unless type callbacks = self.callbacks[type.to_s] return false unless callbacks callbacks.each do |c| c.call(data) end true end
open(_event)
click to toggle source
# File lib/slack/real_time/client.rb, line 118 def open(_event) end
run_loop(socket) { |driver| ... }
click to toggle source
# File lib/slack/real_time/client.rb, line 86 def run_loop(socket) @socket = socket @socket.connect! do |driver| yield driver if block_given? driver.on :open do |event| open(event) callback(event, :open) end driver.on :message do |event| dispatch(event) end driver.on :close do |event| callback(event, :close) close(event) end end end
send_json(data)
click to toggle source
# File lib/slack/real_time/client.rb, line 113 def send_json(data) fail ClientNotStartedError unless started? @socket.send_data(data.to_json) end
socket_class()
click to toggle source
# File lib/slack/real_time/client.rb, line 109 def socket_class concurrency::Socket end
socket_options()
click to toggle source
# File lib/slack/real_time/client.rb, line 79 def socket_options socket_options = {} socket_options[:ping] = websocket_ping if websocket_ping socket_options[:proxy] = websocket_proxy if websocket_proxy socket_options end