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

  • :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)



88
89
90
# File 'lib/wamp_client/connection.rb', line 88

def initialize(options)
  self.options = options || {}
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

Instance Method Details

#_create_sessionObject



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.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



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.options[:transport]
    self.transport = self.options[:transport]
  else
    self.transport = WampClient::Transport::WebSocketTransport.new(self.options)
  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_retryObject



201
202
203
204
# File 'lib/wamp_client/connection.rb', line 201

def _finish_retry
  @retry_timer = 1
  @retrying = false
end

#_retryObject



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

#closeObject

Closes the connection

Raises:

  • (RuntimeError)


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

Returns:

  • (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

#openObject

Opens the connection

Raises:

  • (RuntimeError)


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