Class: WampClient::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)



107
108
109
110
111
# File 'lib/wamp_client/connection.rb', line 107

def initialize(options)
  self.transport_class = options.delete(:transport) || WampClient::Transport::WebSocketEventMachine
  self.options = options || {}
  self.verbose = options[:verbose] || false
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



34
35
36
# File 'lib/wamp_client/connection.rb', line 34

def options
  @options
end

#sessionObject

Returns the value of attribute session.



34
35
36
# File 'lib/wamp_client/connection.rb', line 34

def session
  @session
end

#transportObject

Returns the value of attribute transport.



34
35
36
# File 'lib/wamp_client/connection.rb', line 34

def transport
  @transport
end

#transport_classObject

Returns the value of attribute transport_class.



34
35
36
# File 'lib/wamp_client/connection.rb', line 34

def transport_class
  @transport_class
end

#verboseObject

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_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 = WampClient::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
# 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
    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 |message|
    puts "TRANSPORT ERROR: #{message}"
  end

  @open = true

  self.transport.connect

end

#_finish_retryObject



225
226
227
228
# File 'lib/wamp_client/connection.rb', line 225

def _finish_retry
  @retry_timer = 1
  @retrying = false
end

#_retryObject



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

#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)


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

#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