Class: Spinderella::Client::Broadcaster
- Inherits:
-
Perennial::Protocols::PureRuby::JSONTransport
- Object
- Perennial::Protocols::PureRuby::JSONTransport
- Spinderella::Client::Broadcaster
- Defined in:
- lib/spinderella/client/broadcaster.rb
Instance Method Summary collapse
- #authenticate(token) ⇒ Object
- #authenticated? ⇒ Boolean
- #broadcast_to_all(message) ⇒ Object
- #broadcast_to_channel(message, channel) ⇒ Object
- #broadcast_to_channels(message, channels) ⇒ Object
- #broadcast_to_users(message, users) ⇒ Object
- #close ⇒ Object
-
#initialize(options = {}) ⇒ Broadcaster
constructor
A new instance of Broadcaster.
- #inspect ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Broadcaster
Returns a new instance of Broadcaster.
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/spinderella/client/broadcaster.rb', line 8 def initialize( = {}) @authenticated = false @auth_callback = [] @options = @host = ([:host] || "localhost").to_s @port = ([:port] || 42341).to_i @timeout = ([:timeout] || 15).to_f @read_timeout = ([:read_timeout] || 5).to_f @token = [:token] @mutex = Mutex.new authenticate(@token) if @token end |
Instance Method Details
#authenticate(token) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/spinderella/client/broadcaster.rb', line 21 def authenticate(token) (:authenticate, :token => token) action, payload = (@timeout) @authenticated = (action == "authenticated") if @authenticated @auth_callback.each { |b| b.call } @auth_callback = [] end @authenticated rescue NoConnection @authenticated = false return false end |
#authenticated? ⇒ Boolean
35 36 37 |
# File 'lib/spinderella/client/broadcaster.rb', line 35 def authenticated? !!@authenticated end |
#broadcast_to_all(message) ⇒ Object
44 45 46 47 48 |
# File 'lib/spinderella/client/broadcaster.rb', line 44 def broadcast_to_all() once_authenticated do :broadcast, "type" => "all", "message" => .to_s end end |
#broadcast_to_channel(message, channel) ⇒ Object
64 65 66 67 68 |
# File 'lib/spinderella/client/broadcaster.rb', line 64 def broadcast_to_channel(, channel) once_authenticated do :broadcast, "type" => "channel", "message" => .to_s, "channel" => channel.to_s end end |
#broadcast_to_channels(message, channels) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/spinderella/client/broadcaster.rb', line 57 def broadcast_to_channels(, channels) once_authenticated do :broadcast, "type" => "channels", "message" => .to_s, "channels" => Array(channels).map { |c| c.to_s } end end |
#broadcast_to_users(message, users) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/spinderella/client/broadcaster.rb', line 50 def broadcast_to_users(, users) once_authenticated do :broadcast, "type" => "users", "message" => .to_s, "users" => Array(users).map { |u| u.to_s } end end |
#close ⇒ Object
39 40 41 42 |
# File 'lib/spinderella/client/broadcaster.rb', line 39 def close super @authenticated = false end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/spinderella/client/broadcaster.rb', line 70 def inspect "#<#{self.class.name} authenticated=#{authenticated?}, host=\"#{@host}:#{@port}\" connected=#{alive?}>" end |