Class: Sinatra::RocketIO::Client
- Inherits:
-
Object
- Object
- Sinatra::RocketIO::Client
- Includes:
- EventEmitter
- Defined in:
- lib/sinatra/rocketio/client.rb
Defined Under Namespace
Classes: Error
Instance Attribute Summary collapse
-
#channel ⇒ Object
readonly
Returns the value of attribute channel.
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#settings ⇒ Object
readonly
Returns the value of attribute settings.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
-
#initialize(url, opt = {:type => :websocket, :channel => nil}) ⇒ Client
constructor
A new instance of Client.
- #method_missing(name, *args) ⇒ Object
- #push(type, data) ⇒ Object
- #wait(&block) ⇒ Object
Constructor Details
#initialize(url, opt = {:type => :websocket, :channel => nil}) ⇒ Client
Returns a new instance of Client.
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sinatra/rocketio/client.rb', line 18 def initialize(url, opt={:type => :websocket, :channel => nil}) @url = url @type = opt[:type].to_sym @channel = opt[:channel] ? opt[:channel].to_s : nil @io = nil @settings = nil @ws_close_thread = nil on :__connect do io.push :__channel_id, channel emit :connect end self end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
100 101 102 |
# File 'lib/sinatra/rocketio/client.rb', line 100 def method_missing(name, *args) @io.__send__ name, *args end |
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
15 16 17 |
# File 'lib/sinatra/rocketio/client.rb', line 15 def channel @channel end |
#io ⇒ Object (readonly)
Returns the value of attribute io.
15 16 17 |
# File 'lib/sinatra/rocketio/client.rb', line 15 def io @io end |
#settings ⇒ Object (readonly)
Returns the value of attribute settings.
15 16 17 |
# File 'lib/sinatra/rocketio/client.rb', line 15 def settings @settings end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
15 16 17 |
# File 'lib/sinatra/rocketio/client.rb', line 15 def type @type end |
Instance Method Details
#close ⇒ Object
85 86 87 |
# File 'lib/sinatra/rocketio/client.rb', line 85 def close @io.close if @io end |
#connect ⇒ Object
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/sinatra/rocketio/client.rb', line 52 def connect get_settings unless @settings this = self if @type == :websocket and @settings.include? 'websocket' @io = Sinatra::WebSocketIO::Client.new(@settings['websocket']).connect elsif type == :comet or @settings.include? 'comet' @io = Sinatra::CometIO::Client.new(@settings['comet']).connect @type = :comet else raise Error, "cannot find #{type} IO #{url}" end @io.on :* do |event_name, *args| event_name = :__connect if event_name == :connect this.emit event_name, *args end if @type == :websocket @ws_close_thread = Thread.new do sleep 3 Thread.new do close end emit :error, "websocket port is not open" @type = :comet connect end once :connect do Thread.kill @ws_close_thread if @ws_close_thread @ws_close_thread = nil end end self end |
#push(type, data) ⇒ Object
89 90 91 |
# File 'lib/sinatra/rocketio/client.rb', line 89 def push(type, data) @io.push type, data if @io end |
#wait(&block) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/sinatra/rocketio/client.rb', line 93 def wait(&block) loop do sleep 1 yield if block_given? end end |