Class: Wamp::Client::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/wamp/client/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.

Parameters:

  • options (Hash)

    The different options to pass to the connection

Options Hash (options):

  • :uri (String)

    The uri of the WAMP router to connect to

  • :proxy (String)

    The proxy to get to the router

  • :realm (String)

    The realm to connect to

  • :protocol (String, nil)

    The protocol (default if wamp.2.json)

  • :authid (String, nil)

    The id to authenticate with

  • :authmethods (Array, nil)

    The different auth methods that the client supports

  • :headers (Hash)

    Custom headers to include during the connection

  • :serializer (WampClient::Serializer::Base)

    The serializer to use (default is json)



108
109
110
111
# File 'lib/wamp/client/connection.rb', line 108

def initialize(options)
  self.transport_class = options.delete(:transport) || Wamp::Client::Transport::WebSocketEventMachine
  self.options = options || {}
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



35
36
37
# File 'lib/wamp/client/connection.rb', line 35

def options
  @options
end

#sessionObject

Returns the value of attribute session.



35
36
37
# File 'lib/wamp/client/connection.rb', line 35

def session
  @session
end

#transportObject

Returns the value of attribute transport.



35
36
37
# File 'lib/wamp/client/connection.rb', line 35

def transport
  @transport
end

#transport_classObject

Returns the value of attribute transport_class.



35
36
37
# File 'lib/wamp/client/connection.rb', line 35

def transport_class
  @transport_class
end

Instance Method Details

#_create_sessionObject



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 = Wamp::Client::Session.new(self.transport, self.options)

  # 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.options[:realm])
end

#_create_transportObject



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
224
# 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.options)

  # Setup transport callbacks
  self.transport.on(:open) do

    logger.info("#{self.class.name} TRANSPORT OPEN")

    # Call the callback
    @on_connect.call if @on_connect

    # Create the session
    self._create_session

  end

  self.transport.on(:close) do |reason|
    logger.info("#{self.class.name} TRANSPORT CLOSED: #{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
      self.transport_class.stop_event_machine
    end
  end

  self.transport.on(:error) do |message|
    logger.error("#{self.class.name} TRANSPORT ERROR: #{message}")
  end

  @open = true

  self.transport.connect

end

#_finish_retryObject



226
227
228
229
# File 'lib/wamp/client/connection.rb', line 226

def _finish_retry
  @retry_timer = 1
  @retrying = false
end

#_retryObject



231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/wamp/client/connection.rb', line 231

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

    logger.info("#{self.class.name} RECONNECT in #{@retry_timer} seconds")
    self.transport_class.add_timer(@retry_timer*1000) do
      self._retry if @retrying
    end
  end

end

#closeObject

Closes the connection

Raises:

  • (RuntimeError)


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

Returns:

  • (Boolean)


40
41
42
# File 'lib/wamp/client/connection.rb', line 40

def is_open?
  @open
end

#on(event, &callback) ⇒ Object

Simple setter for callbacks



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wamp/client/connection.rb', line 82

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



62
63
64
# File 'lib/wamp/client/connection.rb', line 62

def on_challenge(&on_challenge)
  @on_challenge = on_challenge
end

#on_connect(&on_connect) ⇒ Object



46
47
48
# File 'lib/wamp/client/connection.rb', line 46

def on_connect(&on_connect)
  @on_connect = on_connect
end

#on_disconnect(&on_disconnect) ⇒ Object



77
78
79
# File 'lib/wamp/client/connection.rb', line 77

def on_disconnect(&on_disconnect)
  @on_disconnect = on_disconnect
end

#on_join(&on_join) ⇒ Object



54
55
56
# File 'lib/wamp/client/connection.rb', line 54

def on_join(&on_join)
  @on_join = on_join
end

#on_leave(&on_leave) ⇒ Object



70
71
72
# File 'lib/wamp/client/connection.rb', line 70

def on_leave(&on_leave)
  @on_leave = on_leave
end

#openObject

Opens the connection

Raises:

  • (RuntimeError)


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