Class: WampClient::Connection
- Inherits:
-
Object
- Object
- WampClient::Connection
- Defined in:
- lib/wamp_client/connection.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#session ⇒ Object
Returns the value of attribute session.
-
#transport ⇒ Object
Returns the value of attribute transport.
-
#transport_class ⇒ Object
Returns the value of attribute transport_class.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Instance Method Summary collapse
- #_create_session ⇒ Object
- #_create_transport ⇒ Object
- #_finish_retry ⇒ Object
- #_retry ⇒ Object
-
#close ⇒ Object
Closes the connection.
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #is_open? ⇒ Boolean
-
#on(event, &callback) ⇒ Object
Simple setter for callbacks.
- #on_challenge(&on_challenge) ⇒ Object
- #on_connect(&on_connect) ⇒ Object
- #on_disconnect(&on_disconnect) ⇒ Object
- #on_join(&on_join) ⇒ Object
- #on_leave(&on_leave) ⇒ Object
-
#open ⇒ Object
Opens the connection.
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
107 108 109 110 111 |
# File 'lib/wamp_client/connection.rb', line 107 def initialize() self.transport_class = .delete(:transport) || WampClient::Transport::WebSocketEventMachine self. = || {} self.verbose = [:verbose] || false end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
34 35 36 |
# File 'lib/wamp_client/connection.rb', line 34 def @options end |
#session ⇒ Object
Returns the value of attribute session.
34 35 36 |
# File 'lib/wamp_client/connection.rb', line 34 def session @session end |
#transport ⇒ Object
Returns the value of attribute transport.
34 35 36 |
# File 'lib/wamp_client/connection.rb', line 34 def transport @transport end |
#transport_class ⇒ Object
Returns the value of attribute transport_class.
34 35 36 |
# File 'lib/wamp_client/connection.rb', line 34 def transport_class @transport_class end |
#verbose ⇒ Object
Returns the value of attribute verbose.
34 35 36 |
# File 'lib/wamp_client/connection.rb', line 34 def verbose @verbose end |
Instance Method Details
#_create_session ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/wamp_client/connection.rb', line 141 def _create_session self.session = WampClient::Session.new(self.transport, self.) # Setup session callbacks self.session.on(:challenge) do |authmethod, extra| self._finish_retry @on_challenge.call(authmethod, extra) if @on_challenge end self.session.on(:join) do |details| self._finish_retry @on_join.call(self.session, details) if @on_join end self.session.on(:leave) do |reason, details| unless @retrying @on_leave.call(reason, details) if @on_leave end if @reconnect # Retry self._retry unless @retrying else # Close the transport self.transport.disconnect end end self.session.join(self.[:realm]) end |
#_create_transport ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/wamp_client/connection.rb', line 173 def _create_transport if self.transport self.transport.disconnect self.transport = nil end # Initialize the transport self.transport = self.transport_class.new(self.) # Setup transport callbacks self.transport.on(:open) do puts "TRANSPORT OPEN" if self.verbose # Call the callback @on_connect.call if @on_connect # Create the session self._create_session end self.transport.on(:close) do |reason| puts "TRANSPORT CLOSED: #{reason}" if self.verbose @open = false unless @retrying @on_disconnect.call(reason) if @on_disconnect end # Nil out the session since the transport closed underneath it self.session = nil if @reconnect # Retry self._retry unless @retrying else # Stop the Event Machine self.transport_class.stop_event_machine end end self.transport.on(:error) do || puts "TRANSPORT ERROR: #{}" end @open = true self.transport.connect end |
#_finish_retry ⇒ Object
225 226 227 228 |
# File 'lib/wamp_client/connection.rb', line 225 def _finish_retry @retry_timer = 1 @retrying = false end |
#_retry ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/wamp_client/connection.rb', line 230 def _retry if self.session == nil or not self.session.is_open? @retry_timer = 2*@retry_timer unless @retry_timer == 32 @retrying = true self._create_transport puts "Attempting Reconnect... Next attempt in #{@retry_timer} seconds" if self.verbose self.transport_class.add_timer(@retry_timer*1000) do self._retry if @retrying end end end |
#close ⇒ Object
Closes the connection
130 131 132 133 134 135 136 137 138 139 |
# File 'lib/wamp_client/connection.rb', line 130 def close raise RuntimeError, 'The connection is already closed' unless self.is_open? # Leave the session @reconnect = false @retrying = false session.leave end |
#is_open? ⇒ Boolean
39 40 41 |
# File 'lib/wamp_client/connection.rb', line 39 def is_open? @open end |
#on(event, &callback) ⇒ Object
Simple setter for callbacks
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/wamp_client/connection.rb', line 81 def on(event, &callback) case event when :connect self.on_connect(&callback) when :join self.on_join(&callback) when :challenge self.on_challenge(&callback) when :leave self.on_leave(&callback) when :disconnect self.on_disconnect(&callback) else raise RuntimeError, "Unknown on(event) '#{event}'" end end |
#on_challenge(&on_challenge) ⇒ Object
61 62 63 |
# File 'lib/wamp_client/connection.rb', line 61 def on_challenge(&on_challenge) @on_challenge = on_challenge end |
#on_connect(&on_connect) ⇒ Object
45 46 47 |
# File 'lib/wamp_client/connection.rb', line 45 def on_connect(&on_connect) @on_connect = on_connect end |
#on_disconnect(&on_disconnect) ⇒ Object
76 77 78 |
# File 'lib/wamp_client/connection.rb', line 76 def on_disconnect(&on_disconnect) @on_disconnect = on_disconnect end |
#on_join(&on_join) ⇒ Object
53 54 55 |
# File 'lib/wamp_client/connection.rb', line 53 def on_join(&on_join) @on_join = on_join end |
#on_leave(&on_leave) ⇒ Object
69 70 71 |
# File 'lib/wamp_client/connection.rb', line 69 def on_leave(&on_leave) @on_leave = on_leave end |
#open ⇒ Object
Opens the connection
114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/wamp_client/connection.rb', line 114 def open raise RuntimeError, 'The connection is already open' if self.is_open? @reconnect = true @retry_timer = 1 @retrying = false self.transport_class.start_event_machine do # Create the transport self._create_transport end end |