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.
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_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.
88 89 90 |
# File 'lib/wamp_client/connection.rb', line 88 def initialize() self. = || {} 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 |
Instance Method Details
#_create_session ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/wamp_client/connection.rb', line 119 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
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 |
# File 'lib/wamp_client/connection.rb', line 151 def _create_transport if self.transport self.transport.disconnect self.transport = nil end # Initialize the transport if self.[:transport] self.transport = self.[:transport] else self.transport = WampClient::Transport::WebSocketTransport.new(self.) end # Setup transport callbacks self.transport.on_open do # Call the callback @on_connect.call if @on_connect # Create the session self._create_session end self.transport.on_close do |reason| @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 EM.stop end end @open = true self.transport.connect end |
#_finish_retry ⇒ Object
201 202 203 204 |
# File 'lib/wamp_client/connection.rb', line 201 def _finish_retry @retry_timer = 1 @retrying = false end |
#_retry ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/wamp_client/connection.rb', line 206 def _retry unless self.session and 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" EM.add_timer(@retry_timer) { self._retry if @retrying } end end |
#close ⇒ Object
Closes the connection
109 110 111 112 113 114 115 116 117 |
# File 'lib/wamp_client/connection.rb', line 109 def close raise RuntimeError, 'The connection is already closed' unless self.is_open? # Leave the session @reconnect = false session.leave end |
#is_open? ⇒ Boolean
39 40 41 |
# File 'lib/wamp_client/connection.rb', line 39 def is_open? @open 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
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/wamp_client/connection.rb', line 93 def open raise RuntimeError, 'The connection is already open' if self.is_open? @reconnect = true @retry_timer = 1 @retrying = false EM.run do # Create the transport self._create_transport end end |