Class: Sinatra::RocketIO::Client

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/sinatra/rocketio/client.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#channelObject (readonly)

Returns the value of attribute channel.



15
16
17
# File 'lib/sinatra/rocketio/client.rb', line 15

def channel
  @channel
end

#ioObject (readonly)

Returns the value of attribute io.



15
16
17
# File 'lib/sinatra/rocketio/client.rb', line 15

def io
  @io
end

#settingsObject (readonly)

Returns the value of attribute settings.



15
16
17
# File 'lib/sinatra/rocketio/client.rb', line 15

def settings
  @settings
end

#typeObject (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

#closeObject



85
86
87
# File 'lib/sinatra/rocketio/client.rb', line 85

def close
  @io.close if @io
end

#connectObject



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