wamp_client 0.0.8 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 89777a1693c71983f0e12863bc34c6c6976549b4
4
- data.tar.gz: 87ffbcbdf160a92bb92436f316b87030cebf5ac3
3
+ metadata.gz: 5401c514eb36f3adbfa52b9f2f5d218a3f884ee5
4
+ data.tar.gz: fae4ea4045398f596d7f67262a121b29c754c724
5
5
  SHA512:
6
- metadata.gz: 516a31f32f64e581290f8629668632510c79ef8bdccc0c5be04e2bb6eb3d8fde65048ec5ec3e74d2522003d71c5dafeaec09b31f9ae70088b9acd57c8e99a393
7
- data.tar.gz: 144113bc2572aebb2d19a883523f50695ad08b0263e842cf3f07863fb683295e84cc5469f4b1b65d052c347f2edcae2cbfd45094b9a9bc23b9c9abd994b5ea6f
6
+ metadata.gz: 833e3a42b79b5de4624fab8ecdc989a4af23db6aba17352ad399f7614e12754ced273cef8419dd257423cfcc736f5ea2ccfb2c4945158b02b07703f650b8736b
7
+ data.tar.gz: 35ccc1c3bead4d40b9ff21c55caf44c69b1ca85474461f43fac9d9fc1777193be4147b2fba4d2e865af7de9b1f3c7879a5d37fc37ecba81c7b9d803d1f871a6f
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2016 Eric Chapman
1
+ Copyright (c) 2018 Eric Chapman
2
2
 
3
3
  MIT License
4
4
 
data/README.md CHANGED
@@ -10,6 +10,10 @@ Please use [wamp_rails](https://github.com/ericchapman/ruby_wamp_rails) to integ
10
10
 
11
11
  ## Revision History
12
12
 
13
+ - v0.0.9:
14
+ - Added support for transport override and 'faye-websocket' transport
15
+ - Added "on(event)" callback (still support legacy methods)
16
+ - Increased Test Coverage for 'Transport' and 'Connection' classes
13
17
  - v0.0.8:
14
18
  - Exposed 'yield' publicly to allow higher level libraries to not use the 'defer'
15
19
  - Removed library version dependency
@@ -70,7 +74,7 @@ options = {
70
74
  }
71
75
  connection = WampClient::Connection.new(options)
72
76
 
73
- connection.on_join do |session, details|
77
+ connection.on(:join) do |session, details|
74
78
  puts "Session Open"
75
79
 
76
80
  # Register for something
@@ -101,46 +105,84 @@ A connection is closed by simply calling "close"
101
105
  connection.close
102
106
  ```
103
107
 
104
- Note that the connection will still call "on_leave" and "on_disconnect" as it closes the session and the transport
108
+ Note that the connection will still call "on(:leave)" and "on(:disconnect)" as it closes the session and the transport
105
109
 
106
110
  #### Callbacks
107
111
  A connection has the following callbacks
108
112
 
109
- **on_connect** - Called when the transport is opened
113
+ **on(:connect)** - Called when the transport is opened
110
114
  ```ruby
111
- connection.on_connect do
115
+ connection.on(:connect) do
112
116
 
113
117
  end
114
118
  ```
115
119
 
116
- **on_join** - Called when the session is established
120
+ **on(:join)** - Called when the session is established
117
121
  ```ruby
118
- connection.on_join do |session, details|
122
+ connection.on(:join) do |session, details|
119
123
 
120
124
  end
121
125
  ```
122
126
 
123
- **on_leave** - Called when the session is terminated
127
+ **on(:leave)** - Called when the session is terminated
124
128
  ```ruby
125
- connection.on_leave do |reason, details|
129
+ connection.on(:leave) do |reason, details|
126
130
 
127
131
  end
128
132
  ```
129
133
 
130
- **on_disconnect** - Called when the connection is terminated
134
+ **on(:disconnect)** - Called when the connection is terminated
131
135
  ```ruby
132
- connection.on_disconnect do |reason|
136
+ connection.on(:disconnect) do |reason|
133
137
 
134
138
  end
135
139
  ```
136
140
 
137
- **on_challenge** - Called when an authentication challenge is created
141
+ **on(:challenge)** - Called when an authentication challenge is created
138
142
  ```ruby
139
- connection.on_challenge do |authmethod, extra|
143
+ connection.on(:challenge) do |authmethod, extra|
140
144
 
141
145
  end
142
146
  ```
143
147
 
148
+ #### Overriding Transport
149
+ By default, the library will use the "websocket-eventmachine-client" Gem as the websocket transport.
150
+ However the library also supports overriding this.
151
+
152
+ ##### GEM: faye-websocket
153
+ To use this library, do the following
154
+
155
+ Install the "faye-websocket" Gem:
156
+
157
+ $ gem install faye-websocket
158
+
159
+ Override the transport by doing the following:
160
+
161
+ ```ruby
162
+ require 'wamp_client'
163
+
164
+ options = {
165
+ uri: 'ws://127.0.0.1:8080/ws',
166
+ realm: 'realm1',
167
+ proxy: { # See faye-websocket documentation
168
+ :origin => 'http://username:password@proxy.example.com',
169
+ :headers => {'User-Agent' => 'ruby'}
170
+ },
171
+ transport: WampClient::Transport::FayeWebSocket
172
+ }
173
+
174
+ connection = WampClient::Connection.new(options)
175
+
176
+ # More code
177
+ ```
178
+
179
+ Note that the "faye-wesbsocket" transport supports passing in a "proxy" as shown above.
180
+
181
+ ##### Custom
182
+ You can also create your own transports by wrapping them in a "Transport" object
183
+ and including as shown above. For more details on this, see the files in
184
+ "lib/wamp_client/transport"
185
+
144
186
  ### Authentication
145
187
  The library supports authentication. Here is how to perform the different methods
146
188
 
@@ -158,7 +200,7 @@ options = {
158
200
  }
159
201
  connection = WampClient::Connection.new(options)
160
202
 
161
- connection.on_challenge do |authmethod, extra|
203
+ connection.on(:challenge) do |authmethod, extra|
162
204
  puts 'Challenge'
163
205
  if authmethod == 'wampcra'
164
206
  WampClient::Auth::Cra.sign('secret', extra[:challenge])
@@ -167,7 +209,7 @@ connection.on_challenge do |authmethod, extra|
167
209
  end
168
210
  end
169
211
 
170
- connection.on_join do |session, details|
212
+ connection.on(:join) do |session, details|
171
213
  puts "Session Open"
172
214
  end
173
215
 
data/lib/wamp_client.rb CHANGED
@@ -28,8 +28,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
28
  require 'wamp_client/version'
29
29
  require 'wamp_client/message'
30
30
  require 'wamp_client/serializer'
31
- require 'wamp_client/transport'
32
31
  require 'wamp_client/connection'
33
32
  require 'wamp_client/session'
34
33
  require 'wamp_client/auth'
35
- require 'wamp_client/defer'
34
+ require 'wamp_client/defer'
@@ -25,13 +25,13 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
25
 
26
26
  =end
27
27
 
28
- require 'websocket-eventmachine-client'
29
28
  require 'wamp_client/session'
30
- require 'wamp_client/transport'
29
+ require 'wamp_client/transport/web_socket_event_machine'
30
+ require 'wamp_client/transport/faye_web_socket'
31
31
 
32
32
  module WampClient
33
33
  class Connection
34
- attr_accessor :options, :transport, :session
34
+ attr_accessor :options, :transport_class, :transport, :session, :verbose
35
35
 
36
36
  @reconnect = true
37
37
 
@@ -77,8 +77,27 @@ module WampClient
77
77
  @on_disconnect = on_disconnect
78
78
  end
79
79
 
80
+ # Simple setter for callbacks
81
+ def on(event, &callback)
82
+ case event
83
+ when :connect
84
+ self.on_connect(&callback)
85
+ when :join
86
+ self.on_join(&callback)
87
+ when :challenge
88
+ self.on_challenge(&callback)
89
+ when :leave
90
+ self.on_leave(&callback)
91
+ when :disconnect
92
+ self.on_disconnect(&callback)
93
+ else
94
+ raise RuntimeError, "Unknown on(event) '#{event}'"
95
+ end
96
+ end
97
+
80
98
  # @param options [Hash] The different options to pass to the connection
81
99
  # @option options [String] :uri The uri of the WAMP router to connect to
100
+ # @option options [String] :proxy The proxy to get to the router
82
101
  # @option options [String] :realm The realm to connect to
83
102
  # @option options [String,nil] :protocol The protocol (default if wamp.2.json)
84
103
  # @option options [String,nil] :authid The id to authenticate with
@@ -86,7 +105,9 @@ module WampClient
86
105
  # @option options [Hash] :headers Custom headers to include during the connection
87
106
  # @option options [WampClient::Serializer::Base] :serializer The serializer to use (default is json)
88
107
  def initialize(options)
108
+ self.transport_class = options.delete(:transport) || WampClient::Transport::WebSocketEventMachine
89
109
  self.options = options || {}
110
+ self.verbose = options[:verbose] || false
90
111
  end
91
112
 
92
113
  # Opens the connection
@@ -98,7 +119,7 @@ module WampClient
98
119
  @retry_timer = 1
99
120
  @retrying = false
100
121
 
101
- EM.run do
122
+ self.transport_class.start_event_machine do
102
123
  # Create the transport
103
124
  self._create_transport
104
125
  end
@@ -112,6 +133,7 @@ module WampClient
112
133
 
113
134
  # Leave the session
114
135
  @reconnect = false
136
+ @retrying = false
115
137
  session.leave
116
138
 
117
139
  end
@@ -120,17 +142,17 @@ module WampClient
120
142
  self.session = WampClient::Session.new(self.transport, self.options)
121
143
 
122
144
  # Setup session callbacks
123
- self.session.on_challenge do |authmethod, extra|
145
+ self.session.on(:challenge) do |authmethod, extra|
124
146
  self._finish_retry
125
147
  @on_challenge.call(authmethod, extra) if @on_challenge
126
148
  end
127
149
 
128
- self.session.on_join do |details|
150
+ self.session.on(:join) do |details|
129
151
  self._finish_retry
130
152
  @on_join.call(self.session, details) if @on_join
131
153
  end
132
154
 
133
- self.session.on_leave do |reason, details|
155
+ self.session.on(:leave) do |reason, details|
134
156
 
135
157
  unless @retrying
136
158
  @on_leave.call(reason, details) if @on_leave
@@ -156,14 +178,11 @@ module WampClient
156
178
  end
157
179
 
158
180
  # Initialize the transport
159
- if self.options[:transport]
160
- self.transport = self.options[:transport]
161
- else
162
- self.transport = WampClient::Transport::WebSocketTransport.new(self.options)
163
- end
181
+ self.transport = self.transport_class.new(self.options)
164
182
 
165
183
  # Setup transport callbacks
166
- self.transport.on_open do
184
+ self.transport.on(:open) do
185
+ puts "TRANSPORT OPEN" if self.verbose
167
186
 
168
187
  # Call the callback
169
188
  @on_connect.call if @on_connect
@@ -173,7 +192,8 @@ module WampClient
173
192
 
174
193
  end
175
194
 
176
- self.transport.on_close do |reason|
195
+ self.transport.on(:close) do |reason|
196
+ puts "TRANSPORT CLOSED: #{reason}" if self.verbose
177
197
  @open = false
178
198
 
179
199
  unless @retrying
@@ -188,10 +208,14 @@ module WampClient
188
208
  self._retry unless @retrying
189
209
  else
190
210
  # Stop the Event Machine
191
- EM.stop
211
+ self.transport_class.stop_event_machine
192
212
  end
193
213
  end
194
214
 
215
+ self.transport.on(:error) do |message|
216
+ puts "TRANSPORT ERROR: #{message}"
217
+ end
218
+
195
219
  @open = true
196
220
 
197
221
  self.transport.connect
@@ -205,16 +229,16 @@ module WampClient
205
229
 
206
230
  def _retry
207
231
 
208
- unless self.session and self.session.is_open?
232
+ if self.session == nil or not self.session.is_open?
209
233
  @retry_timer = 2*@retry_timer unless @retry_timer == 32
210
234
  @retrying = true
211
235
 
212
236
  self._create_transport
213
237
 
214
- puts "Attempting Reconnect... Next attempt in #{@retry_timer} seconds"
215
- EM.add_timer(@retry_timer) {
238
+ puts "Attempting Reconnect... Next attempt in #{@retry_timer} seconds" if self.verbose
239
+ self.transport_class.add_timer(@retry_timer*1000) do
216
240
  self._retry if @retrying
217
- }
241
+ end
218
242
  end
219
243
 
220
244
  end
@@ -25,7 +25,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25
25
 
26
26
  =end
27
27
 
28
- require 'wamp_client/transport'
28
+ require 'wamp_client/transport/base'
29
29
  require 'wamp_client/message'
30
30
  require 'wamp_client/check'
31
31
  require 'wamp_client/version'
@@ -167,6 +167,20 @@ module WampClient
167
167
  @on_challenge = on_challenge
168
168
  end
169
169
 
170
+ # Simple setter for callbacks
171
+ def on(event, &callback)
172
+ case event
173
+ when :join
174
+ self.on_join(&callback)
175
+ when :challenge
176
+ self.on_challenge(&callback)
177
+ when :leave
178
+ self.on_leave(&callback)
179
+ else
180
+ raise RuntimeError, "Unknown on(event) '#{event}'"
181
+ end
182
+ end
183
+
170
184
  attr_accessor :id, :realm, :transport, :verbose, :options
171
185
 
172
186
  # Private attributes
@@ -212,6 +226,7 @@ module WampClient
212
226
  # Setup session callbacks
213
227
  @on_join = nil
214
228
  @on_leave = nil
229
+ @on_challenge = nil
215
230
 
216
231
  end
217
232
 
@@ -908,7 +923,7 @@ module WampClient
908
923
 
909
924
  # Timeout Logic
910
925
  if options[:timeout] and options[:timeout] > 0
911
- self.transport.timer(options[:timeout]) do
926
+ self.transport.add_timer(options[:timeout]) do
912
927
  # Once the timer expires, if the call hasn't completed, cancel it
913
928
  if self._requests[:call][call.id]
914
929
  call.cancel
@@ -52,16 +52,34 @@ module WampClient
52
52
  end
53
53
 
54
54
  # Callback when there is an error. Parameters are
55
+ # @param msg [String] The message from the error
55
56
  @on_error
56
57
  def on_error(&on_error)
57
58
  @on_error = on_error
58
59
  end
59
60
 
60
- attr_accessor :type, :uri, :headers, :protocol, :serializer, :connected
61
+ # Simple setter for callbacks
62
+ def on(event, &callback)
63
+ case event
64
+ when :open
65
+ self.on_open(&callback)
66
+ when :close
67
+ self.on_close(&callback)
68
+ when :message
69
+ self.on_message(&callback)
70
+ when :error
71
+ self.on_error(&callback)
72
+ else
73
+ raise RuntimeError, "Unknown on(event) '#{event}'"
74
+ end
75
+ end
76
+
77
+ attr_accessor :uri, :proxy, :headers, :protocol, :serializer, :connected
61
78
 
62
79
  # Constructor for the transport
63
80
  # @param options [Hash] The connection options. the different options are as follows
64
81
  # @option options [String] :uri The url to connect to
82
+ # @option options [String] :proxy The proxy to use
65
83
  # @option options [String] :protocol The protocol
66
84
  # @option options [Hash] :headers Custom headers to include during the connection
67
85
  # @option options [WampClient::Serializer::Base] :serializer The serializer to use
@@ -70,6 +88,7 @@ module WampClient
70
88
  # Initialize the parameters
71
89
  self.connected = false
72
90
  self.uri = options[:uri]
91
+ self.proxy = options[:proxy]
73
92
  self.headers = options[:headers] || {}
74
93
  self.protocol = options[:protocol] || 'wamp.2.json'
75
94
  self.serializer = options[:serializer] || WampClient::Serializer::JSONSerializer.new
@@ -109,62 +128,24 @@ module WampClient
109
128
  # Process the callback when the timer expires
110
129
  # @param [Integer] milliseconds - The number
111
130
  # @param [block] callback - The callback that is fired when the timer expires
112
- def timer(milliseconds, &callback)
131
+ def self.add_timer(milliseconds, &callback)
113
132
  # Implement in subclass
114
133
  end
115
-
116
- end
117
-
118
- # This implementation uses the 'websocket-eventmachine-client' Gem. This is the default if no transport is included
119
- class WebSocketTransport < Base
120
- attr_accessor :socket
121
-
122
- def initialize(options)
123
- super(options)
124
- self.type = 'websocket'
125
- self.socket = nil
126
- end
127
-
128
- def connect
129
- self.socket = WebSocket::EventMachine::Client.connect(
130
- :uri => self.uri,
131
- :headers => self.headers
132
- )
133
-
134
- self.socket.onopen do
135
- self.connected = true
136
- @on_open.call unless @on_open.nil?
137
- end
138
-
139
- self.socket.onmessage do |msg, type|
140
- @on_message.call(self.serializer.deserialize(msg)) unless @on_message.nil?
141
- end
142
-
143
- self.socket.onclose do |code, reason|
144
- self.connected = false
145
- @on_close.call(reason) unless @on_close.nil?
146
- end
147
- end
148
-
149
- def disconnect
150
- self.connected = !self.socket.close # close returns 'true' if the connection was closed immediately
134
+ def add_timer(milliseconds, &callback)
135
+ self.class.add_timer(milliseconds, &callback)
151
136
  end
152
137
 
153
- def send_message(msg)
154
- if self.connected
155
- self.socket.send(self.serializer.serialize(msg), {type: 'text'})
156
- else
157
- raise RuntimeError, "Socket must be open to call 'send_message'"
158
- end
138
+ # Method to start the event machine for the socket
139
+ def self.start_event_machine(&block)
140
+ # Implement in subclass
159
141
  end
160
142
 
161
- def timer(milliseconds, &callback)
162
- delay = (milliseconds.to_f/1000.0).ceil
163
- EM.add_timer(delay) {
164
- callback.call
165
- }
143
+ # Method to stop the vent machine
144
+ def self.stop_event_machine
145
+ # Implement in subclass
166
146
  end
167
147
 
168
148
  end
149
+
169
150
  end
170
151
  end