Class: EventMachine::RocketIO::Client

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/em-rocketio-client.rb,
lib/em-rocketio-client/client.rb,
lib/em-rocketio-client/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, opt = {:type => :websocket, :channel => nil}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/em-rocketio-client/client.rb', line 15

def initialize(url, opt={:type => :websocket, :channel => nil})
  @url = url
  @type = opt[:type].to_sym
  @channel = opt[:channel] ? opt[:channel].to_s : nil
  @settings = nil
  @io = nil
  @ws_close_timer = nil
  on :__connect do
    io.push :__channel_id, channel
    emit :connect
  end
  get_settings
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



104
105
106
# File 'lib/em-rocketio-client/client.rb', line 104

def method_missing(name, *args)
  @io.__send__ name, *args
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



8
9
10
# File 'lib/em-rocketio-client/client.rb', line 8

def channel
  @channel
end

#ioObject (readonly)

Returns the value of attribute io.



8
9
10
# File 'lib/em-rocketio-client/client.rb', line 8

def io
  @io
end

#settingsObject (readonly)

Returns the value of attribute settings.



8
9
10
# File 'lib/em-rocketio-client/client.rb', line 8

def settings
  @settings
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/em-rocketio-client/client.rb', line 8

def type
  @type
end

Class Method Details

.settingsObject



10
11
12
# File 'lib/em-rocketio-client/client.rb', line 10

def self.settings
  @@settings ||= {}
end

Instance Method Details

#closeObject



96
97
98
# File 'lib/em-rocketio-client/client.rb', line 96

def close
  @io.close
end

#connectObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/em-rocketio-client/client.rb', line 64

def connect
  this = self
  once :__settings do
    if @type == :websocket and @settings.include? 'websocket'
      @io = EM::WebSocketIO::Client.new(@settings['websocket']).connect
    elsif @type == :comet or @settings.include? 'comet'
      @io = EM::CometIO::Client.new(@settings['comet']).connect
      @type = :comet
    else
      raise Error, "cannnot found #{@type} IO"
    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_timer = EM::add_timer 3 do
        close
        emit :error, "websocket port is not open"
        @type = :comet
        connect
      end
      once :connect do
        EM::cancel_timer @ws_close_timer if @ws_close_timer
        @ws_close_timer = nil
      end
    end
  end
  emit :__settings if @settings
  self
end

#push(type, data = {}) ⇒ Object



100
101
102
# File 'lib/em-rocketio-client/client.rb', line 100

def push(type, data={})
  @io.push type, data if @io
end