wamp_client 0.0.9 → 0.1.0
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 +4 -4
- data/README.md +28 -26
- data/lib/{wamp_client.rb → wamp/client.rb} +8 -8
- data/lib/{wamp_client → wamp/client}/auth.rb +13 -11
- data/lib/wamp/client/check.rb +86 -0
- data/lib/wamp/client/connection.rb +249 -0
- data/lib/{wamp_client → wamp/client}/defer.rb +29 -27
- data/lib/wamp/client/message.rb +1322 -0
- data/lib/{wamp_client → wamp/client}/serializer.rb +26 -24
- data/lib/wamp/client/session.rb +1001 -0
- data/lib/wamp/client/transport/base.rb +152 -0
- data/lib/{wamp_client → wamp/client}/transport/event_machine_base.rb +19 -17
- data/lib/wamp/client/transport/faye_web_socket.rb +85 -0
- data/lib/wamp/client/transport/web_socket_event_machine.rb +88 -0
- data/lib/{wamp_client → wamp/client}/version.rb +5 -3
- data/scripts/gen_message.rb +54 -53
- data/spec/spec_helper.rb +3 -3
- data/spec/{auth_spec.rb → wamp/client/auth_spec.rb} +2 -2
- data/spec/{check_spec.rb → wamp/client/check_spec.rb} +2 -2
- data/spec/{connection_spec.rb → wamp/client/connection_spec.rb} +7 -7
- data/spec/{message_spec.rb → wamp/client/message_spec.rb} +298 -298
- data/spec/{session_spec.rb → wamp/client/session_spec.rb} +134 -134
- data/spec/{transport_spec.rb → wamp/client/transport_spec.rb} +4 -4
- data/wamp_client.gemspec +2 -2
- metadata +50 -50
- data/lib/wamp_client/check.rb +0 -84
- data/lib/wamp_client/connection.rb +0 -247
- data/lib/wamp_client/message.rb +0 -1348
- data/lib/wamp_client/session.rb +0 -1000
- data/lib/wamp_client/transport/base.rb +0 -151
- data/lib/wamp_client/transport/faye_web_socket.rb +0 -83
- data/lib/wamp_client/transport/web_socket_event_machine.rb +0 -86
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 461e1109df53efb8a73f4baa59154764499dc5b4
|
4
|
+
data.tar.gz: 20bfbab92bcfa98139f4eaa21b00da249cbb0e3b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92d7ade25879d7928d27aa83380d5f5196962fa241c325f556f4ae2da0fede72aa8198ef46a60f13d643c4121c4cc83d5bc7bc51fc5b3ee5ad8f8a79c5583335
|
7
|
+
data.tar.gz: 8da8ebf72fabfe7edf6a4700233d7ee076b7d7d4e89295c4c42748059c30ef423c15c70c94802a815d2f7b50d2741aa6b8059bd5efb88dc4c382318f67182a3c
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# Wamp::Client
|
2
2
|
|
3
3
|
[](https://badge.fury.io/rb/wamp_client)
|
4
4
|
[](https://circleci.com/gh/ericchapman/ruby_wamp_client/tree/master)
|
@@ -10,6 +10,8 @@ Please use [wamp_rails](https://github.com/ericchapman/ruby_wamp_rails) to integ
|
|
10
10
|
|
11
11
|
## Revision History
|
12
12
|
|
13
|
+
- v0.1.0:
|
14
|
+
- BREAKING CHANGE - Changed all of the namespaces to be "Wamp::Client"
|
13
15
|
- v0.0.9:
|
14
16
|
- Added support for transport override and 'faye-websocket' transport
|
15
17
|
- Added "on(event)" callback (still support legacy methods)
|
@@ -65,14 +67,14 @@ is open
|
|
65
67
|
A connection can be created as follows
|
66
68
|
|
67
69
|
```ruby
|
68
|
-
require '
|
70
|
+
require 'wamp/client'
|
69
71
|
|
70
72
|
options = {
|
71
73
|
uri: 'ws://127.0.0.1:8080/ws',
|
72
74
|
realm: 'realm1',
|
73
75
|
verbose: true
|
74
76
|
}
|
75
|
-
connection =
|
77
|
+
connection = Wamp::Client::Connection.new(options)
|
76
78
|
|
77
79
|
connection.on(:join) do |session, details|
|
78
80
|
puts "Session Open"
|
@@ -159,7 +161,7 @@ Install the "faye-websocket" Gem:
|
|
159
161
|
Override the transport by doing the following:
|
160
162
|
|
161
163
|
```ruby
|
162
|
-
require '
|
164
|
+
require 'wamp/client'
|
163
165
|
|
164
166
|
options = {
|
165
167
|
uri: 'ws://127.0.0.1:8080/ws',
|
@@ -168,10 +170,10 @@ options = {
|
|
168
170
|
:origin => 'http://username:password@proxy.example.com',
|
169
171
|
:headers => {'User-Agent' => 'ruby'}
|
170
172
|
},
|
171
|
-
transport:
|
173
|
+
transport: Wamp::Client::Transport::FayeWebSocket
|
172
174
|
}
|
173
175
|
|
174
|
-
connection =
|
176
|
+
connection = Wamp::Client::Connection.new(options)
|
175
177
|
|
176
178
|
# More code
|
177
179
|
```
|
@@ -190,7 +192,7 @@ The library supports authentication. Here is how to perform the different metho
|
|
190
192
|
To perform WAMP CRA, do the following
|
191
193
|
|
192
194
|
```ruby
|
193
|
-
require '
|
195
|
+
require 'wamp/client'
|
194
196
|
|
195
197
|
options = {
|
196
198
|
uri: 'ws://127.0.0.1:8080/ws',
|
@@ -198,12 +200,12 @@ options = {
|
|
198
200
|
authid: 'joe',
|
199
201
|
authmethods: ['wampcra']
|
200
202
|
}
|
201
|
-
connection =
|
203
|
+
connection = Wamp::Client::Connection.new(options)
|
202
204
|
|
203
205
|
connection.on(:challenge) do |authmethod, extra|
|
204
206
|
puts 'Challenge'
|
205
207
|
if authmethod == 'wampcra'
|
206
|
-
|
208
|
+
Wamp::Client::Auth::Cra.sign('secret', extra[:challenge])
|
207
209
|
else
|
208
210
|
raise RuntimeError, "Unsupported auth method #{authmethod}"
|
209
211
|
end
|
@@ -231,7 +233,7 @@ All handlers are called with the following parameters
|
|
231
233
|
- args [Array] - Array of arguments
|
232
234
|
- kwargs [Hash] - Hash of key/value arguments
|
233
235
|
- details [Hash] - Hash containing some details about the call. Details include
|
234
|
-
- session [
|
236
|
+
- session [Wamp::Client::Session] - The session
|
235
237
|
- etc.
|
236
238
|
|
237
239
|
Some examples of this are shown below
|
@@ -261,7 +263,7 @@ All callbacks are called with the following parameters
|
|
261
263
|
- error [Hash] - Hash containing "error", "args", and "kwargs" if an error occurred
|
262
264
|
- details [Hash] - Hash containing some details about the call. Details include
|
263
265
|
- type [String] - The type of message
|
264
|
-
- session [
|
266
|
+
- session [Wamp::Client::Session] - The session
|
265
267
|
- etc.
|
266
268
|
|
267
269
|
An example of this is shown below
|
@@ -503,9 +505,9 @@ Errors can either be raised OR returned as shown below
|
|
503
505
|
handler = lambda do |args, kwargs, details|
|
504
506
|
raise 'error'
|
505
507
|
# OR
|
506
|
-
raise
|
508
|
+
raise Wamp::Client::CallError.new('wamp.error', ['some error'], {details: true})
|
507
509
|
# OR
|
508
|
-
|
510
|
+
Wamp::Client::CallError.new('wamp.error', ['some error'], {details: true})
|
509
511
|
end
|
510
512
|
session.register('com.example.procedure', handler)
|
511
513
|
```
|
@@ -518,7 +520,7 @@ caller. This is shown below
|
|
518
520
|
|
519
521
|
```ruby
|
520
522
|
def add(args, kwargs, details)
|
521
|
-
defer =
|
523
|
+
defer = Wamp::Client::Defer::CallDefer.new
|
522
524
|
EM.add_timer(2) { # Something Async
|
523
525
|
defer.succeed(args[0]+args[1])
|
524
526
|
}
|
@@ -531,9 +533,9 @@ Errors are returned as follows
|
|
531
533
|
|
532
534
|
```ruby
|
533
535
|
def add(args, kwargs, details)
|
534
|
-
defer =
|
536
|
+
defer = Wamp::Client::Defer::CallDefer.new
|
535
537
|
EM.add_timer(2) { # Something Async
|
536
|
-
defer.fail(
|
538
|
+
defer.fail(Wamp::Client::CallError.new('test.error'))
|
537
539
|
}
|
538
540
|
defer
|
539
541
|
end
|
@@ -559,15 +561,15 @@ end
|
|
559
561
|
|
560
562
|
```ruby
|
561
563
|
def add(args, kwargs, details)
|
562
|
-
defer =
|
564
|
+
defer = Wamp::Client::Defer::ProgressiveCallDefer.new
|
563
565
|
EM.add_timer(2) { # Something Async
|
564
|
-
defer.progress(
|
566
|
+
defer.progress(Wamp::Client::CallResult.new([1,2,3]))
|
565
567
|
}
|
566
568
|
EM.add_timer(4) { # Something Async
|
567
|
-
defer.progress(
|
569
|
+
defer.progress(Wamp::Client::CallResult.new([4,5,6]))
|
568
570
|
}
|
569
571
|
EM.add_timer(6) { # Something Async
|
570
|
-
defer.succeed(
|
572
|
+
defer.succeed(Wamp::Client::CallResult.new)
|
571
573
|
}
|
572
574
|
defer
|
573
575
|
end
|
@@ -610,20 +612,20 @@ def interrupt_handler(request, mode)
|
|
610
612
|
end
|
611
613
|
|
612
614
|
def add(args, kwargs, details)
|
613
|
-
defer =
|
615
|
+
defer = Wamp::Client::Defer::ProgressiveCallDefer.new
|
614
616
|
EM.add_timer(2) { # Something Async
|
615
617
|
if @interrupts[defer.request].nil?
|
616
|
-
defer.progress(
|
618
|
+
defer.progress(Wamp::Client::CallResult.new([1,2,3]))
|
617
619
|
end
|
618
620
|
}
|
619
621
|
EM.add_timer(4) { # Something Async
|
620
622
|
if @interrupts[defer.request].nil?
|
621
|
-
defer.progress(
|
623
|
+
defer.progress(Wamp::Client::CallResult.new([4,5,6]))
|
622
624
|
end
|
623
625
|
}
|
624
626
|
EM.add_timer(6) { # Something Async
|
625
627
|
if @interrupts[defer.request].nil?
|
626
|
-
defer.succeed(
|
628
|
+
defer.succeed(Wamp::Client::CallResult.new)
|
627
629
|
end
|
628
630
|
@interrupts.delete(request)
|
629
631
|
}
|
@@ -663,8 +665,8 @@ The *lib/wamp_client/message.rb* file and the *spec/message_spec.rb* file are au
|
|
663
665
|
|
664
666
|
$ cd scripts
|
665
667
|
$ ./gen_message.rb
|
666
|
-
$ mv message.rb.tmp ../lib/
|
667
|
-
$ mv message_spec.rb.tmp ../spec/message_spec.rb
|
668
|
+
$ mv message.rb.tmp ../lib/wamp/client/message.rb
|
669
|
+
$ mv message_spec.rb.tmp ../spec/wamp/client/message_spec.rb
|
668
670
|
|
669
671
|
As I was writing the code for the messages I caught myself cutting and pasting allot and decided these would be
|
670
672
|
better suited to be autogenerated.
|
@@ -1,6 +1,6 @@
|
|
1
1
|
=begin
|
2
2
|
|
3
|
-
Copyright (c)
|
3
|
+
Copyright (c) 2018 Eric Chapman
|
4
4
|
|
5
5
|
MIT License
|
6
6
|
|
@@ -25,10 +25,10 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
25
25
|
|
26
26
|
=end
|
27
27
|
|
28
|
-
require '
|
29
|
-
require '
|
30
|
-
require '
|
31
|
-
require '
|
32
|
-
require '
|
33
|
-
require '
|
34
|
-
require '
|
28
|
+
require 'wamp/client/version'
|
29
|
+
require 'wamp/client/message'
|
30
|
+
require 'wamp/client/serializer'
|
31
|
+
require 'wamp/client/connection'
|
32
|
+
require 'wamp/client/session'
|
33
|
+
require 'wamp/client/auth'
|
34
|
+
require 'wamp/client/defer'
|
@@ -1,6 +1,6 @@
|
|
1
1
|
=begin
|
2
2
|
|
3
|
-
Copyright (c)
|
3
|
+
Copyright (c) 2018 Eric Chapman
|
4
4
|
|
5
5
|
MIT License
|
6
6
|
|
@@ -28,18 +28,20 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
28
28
|
require 'openssl'
|
29
29
|
require 'base64'
|
30
30
|
|
31
|
-
module
|
32
|
-
module
|
33
|
-
module
|
31
|
+
module Wamp
|
32
|
+
module Client
|
33
|
+
module Auth
|
34
|
+
module Cra
|
34
35
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
36
|
+
# Generates the signature from the challenge
|
37
|
+
# @param key [String]
|
38
|
+
# @param challenge [String]
|
39
|
+
def self.sign(key, challenge)
|
40
|
+
hash = OpenSSL::HMAC.digest('sha256', key, challenge)
|
41
|
+
Base64.encode64(hash).gsub(/\n/,'')
|
42
|
+
end
|
42
43
|
|
44
|
+
end
|
43
45
|
end
|
44
46
|
end
|
45
47
|
end
|
@@ -0,0 +1,86 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
Copyright (c) 2018 Eric Chapman
|
4
|
+
|
5
|
+
MIT License
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
8
|
+
a copy of this software and associated documentation files (the
|
9
|
+
"Software"), to deal in the Software without restriction, including
|
10
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
11
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
+
permit persons to whom the Software is furnished to do so, subject to
|
13
|
+
the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be
|
16
|
+
included in all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
22
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
|
26
|
+
=end
|
27
|
+
|
28
|
+
module Wamp
|
29
|
+
module Client
|
30
|
+
module Check
|
31
|
+
|
32
|
+
def self.included(base)
|
33
|
+
base.extend(ClassMethods)
|
34
|
+
end
|
35
|
+
|
36
|
+
module ClassMethods
|
37
|
+
|
38
|
+
def check_equal(name, expected, value)
|
39
|
+
raise ArgumentError, "The '#{name}' argument must have the value '#{expected}'. Instead the value was '#{value}'" unless value == expected
|
40
|
+
end
|
41
|
+
|
42
|
+
def check_gte(name, expected, value)
|
43
|
+
raise ArgumentError, "The '#{name}' argument must be greater than or equal to '#{expected}'. Instead the value was '#{value}'" unless value >= expected
|
44
|
+
end
|
45
|
+
|
46
|
+
def check_nil(name, param, nil_allowed)
|
47
|
+
raise ArgumentError, "The '#{name}' argument cannot be nil" if param.nil? and not nil_allowed
|
48
|
+
end
|
49
|
+
|
50
|
+
def check_int(name, param, nil_allowed=false)
|
51
|
+
check_nil(name, param, nil_allowed)
|
52
|
+
raise ArgumentError, "The '#{name}' argument must be an integer" unless param.nil? or param.is_a? Integer
|
53
|
+
end
|
54
|
+
|
55
|
+
def check_string(name, param, nil_allowed=false)
|
56
|
+
check_nil(name, param, nil_allowed)
|
57
|
+
raise ArgumentError, "The '#{name}' argument must be a string" unless param.nil? or param.is_a? String
|
58
|
+
end
|
59
|
+
|
60
|
+
def check_bool(name, param, nil_allowed=false)
|
61
|
+
check_nil(name, param, nil_allowed)
|
62
|
+
raise ArgumentError, "The '#{name}' argument must be a boolean" unless param.nil? or !!param == param
|
63
|
+
end
|
64
|
+
|
65
|
+
def check_dict(name, param, nil_allowed=false)
|
66
|
+
check_nil(name, param, nil_allowed)
|
67
|
+
raise ArgumentError, "The '#{name}' argument must be a hash" unless param.nil? or param.is_a? Hash
|
68
|
+
end
|
69
|
+
|
70
|
+
def check_list(name, param, nil_allowed=false)
|
71
|
+
check_nil(name, param, nil_allowed)
|
72
|
+
raise ArgumentError, "The '#{name}' argument must be an array" unless param.nil? or param.is_a? Array
|
73
|
+
end
|
74
|
+
|
75
|
+
def check_uri(name, param, nil_allowed=false)
|
76
|
+
check_string(name, param, nil_allowed)
|
77
|
+
end
|
78
|
+
|
79
|
+
def check_id(name, param, nil_allowed=false)
|
80
|
+
check_int(name, param, nil_allowed)
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
@@ -0,0 +1,249 @@
|
|
1
|
+
=begin
|
2
|
+
|
3
|
+
Copyright (c) 2018 Eric Chapman
|
4
|
+
|
5
|
+
MIT License
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
8
|
+
a copy of this software and associated documentation files (the
|
9
|
+
"Software"), to deal in the Software without restriction, including
|
10
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
11
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
12
|
+
permit persons to whom the Software is furnished to do so, subject to
|
13
|
+
the following conditions:
|
14
|
+
|
15
|
+
The above copyright notice and this permission notice shall be
|
16
|
+
included in all copies or substantial portions of the Software.
|
17
|
+
|
18
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
19
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
20
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
21
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
22
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
24
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
25
|
+
|
26
|
+
=end
|
27
|
+
|
28
|
+
require 'wamp/client/session'
|
29
|
+
require 'wamp/client/transport/web_socket_event_machine'
|
30
|
+
require 'wamp/client/transport/faye_web_socket'
|
31
|
+
|
32
|
+
module Wamp
|
33
|
+
module Client
|
34
|
+
class Connection
|
35
|
+
attr_accessor :options, :transport_class, :transport, :session, :verbose
|
36
|
+
|
37
|
+
@reconnect = true
|
38
|
+
|
39
|
+
@open = false
|
40
|
+
def is_open?
|
41
|
+
@open
|
42
|
+
end
|
43
|
+
|
44
|
+
# Called when the connection is established
|
45
|
+
@on_connect
|
46
|
+
def on_connect(&on_connect)
|
47
|
+
@on_connect = on_connect
|
48
|
+
end
|
49
|
+
|
50
|
+
# Called when the WAMP session is established
|
51
|
+
# @param session [WampClient::Session]
|
52
|
+
# @param details [Hash]
|
53
|
+
@on_join
|
54
|
+
def on_join(&on_join)
|
55
|
+
@on_join = on_join
|
56
|
+
end
|
57
|
+
|
58
|
+
# Called when the WAMP session presents a challenge
|
59
|
+
# @param authmethod [String]
|
60
|
+
# @param extra [Hash]
|
61
|
+
@on_challenge
|
62
|
+
def on_challenge(&on_challenge)
|
63
|
+
@on_challenge = on_challenge
|
64
|
+
end
|
65
|
+
|
66
|
+
# Called when the WAMP session is terminated
|
67
|
+
# @param reason [String] The reason the session left the router
|
68
|
+
# @param details [Hash] Object containing information about the left session
|
69
|
+
@on_leave
|
70
|
+
def on_leave(&on_leave)
|
71
|
+
@on_leave = on_leave
|
72
|
+
end
|
73
|
+
|
74
|
+
# Called when the connection is terminated
|
75
|
+
# @param reason [String] The reason the transport was disconnected
|
76
|
+
@on_disconnect
|
77
|
+
def on_disconnect(&on_disconnect)
|
78
|
+
@on_disconnect = on_disconnect
|
79
|
+
end
|
80
|
+
|
81
|
+
# Simple setter for callbacks
|
82
|
+
def on(event, &callback)
|
83
|
+
case event
|
84
|
+
when :connect
|
85
|
+
self.on_connect(&callback)
|
86
|
+
when :join
|
87
|
+
self.on_join(&callback)
|
88
|
+
when :challenge
|
89
|
+
self.on_challenge(&callback)
|
90
|
+
when :leave
|
91
|
+
self.on_leave(&callback)
|
92
|
+
when :disconnect
|
93
|
+
self.on_disconnect(&callback)
|
94
|
+
else
|
95
|
+
raise RuntimeError, "Unknown on(event) '#{event}'"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# @param options [Hash] The different options to pass to the connection
|
100
|
+
# @option options [String] :uri The uri of the WAMP router to connect to
|
101
|
+
# @option options [String] :proxy The proxy to get to the router
|
102
|
+
# @option options [String] :realm The realm to connect to
|
103
|
+
# @option options [String,nil] :protocol The protocol (default if wamp.2.json)
|
104
|
+
# @option options [String,nil] :authid The id to authenticate with
|
105
|
+
# @option options [Array, nil] :authmethods The different auth methods that the client supports
|
106
|
+
# @option options [Hash] :headers Custom headers to include during the connection
|
107
|
+
# @option options [WampClient::Serializer::Base] :serializer The serializer to use (default is json)
|
108
|
+
def initialize(options)
|
109
|
+
self.transport_class = options.delete(:transport) || Wamp::Client::Transport::WebSocketEventMachine
|
110
|
+
self.options = options || {}
|
111
|
+
self.verbose = options[:verbose] || false
|
112
|
+
end
|
113
|
+
|
114
|
+
# Opens the connection
|
115
|
+
def open
|
116
|
+
|
117
|
+
raise RuntimeError, 'The connection is already open' if self.is_open?
|
118
|
+
|
119
|
+
@reconnect = true
|
120
|
+
@retry_timer = 1
|
121
|
+
@retrying = false
|
122
|
+
|
123
|
+
self.transport_class.start_event_machine do
|
124
|
+
# Create the transport
|
125
|
+
self._create_transport
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
|
130
|
+
# Closes the connection
|
131
|
+
def close
|
132
|
+
|
133
|
+
raise RuntimeError, 'The connection is already closed' unless self.is_open?
|
134
|
+
|
135
|
+
# Leave the session
|
136
|
+
@reconnect = false
|
137
|
+
@retrying = false
|
138
|
+
session.leave
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
def _create_session
|
143
|
+
self.session = Wamp::Client::Session.new(self.transport, self.options)
|
144
|
+
|
145
|
+
# Setup session callbacks
|
146
|
+
self.session.on(:challenge) do |authmethod, extra|
|
147
|
+
self._finish_retry
|
148
|
+
@on_challenge.call(authmethod, extra) if @on_challenge
|
149
|
+
end
|
150
|
+
|
151
|
+
self.session.on(:join) do |details|
|
152
|
+
self._finish_retry
|
153
|
+
@on_join.call(self.session, details) if @on_join
|
154
|
+
end
|
155
|
+
|
156
|
+
self.session.on(:leave) do |reason, details|
|
157
|
+
|
158
|
+
unless @retrying
|
159
|
+
@on_leave.call(reason, details) if @on_leave
|
160
|
+
end
|
161
|
+
|
162
|
+
if @reconnect
|
163
|
+
# Retry
|
164
|
+
self._retry unless @retrying
|
165
|
+
else
|
166
|
+
# Close the transport
|
167
|
+
self.transport.disconnect
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
self.session.join(self.options[:realm])
|
172
|
+
end
|
173
|
+
|
174
|
+
def _create_transport
|
175
|
+
|
176
|
+
if self.transport
|
177
|
+
self.transport.disconnect
|
178
|
+
self.transport = nil
|
179
|
+
end
|
180
|
+
|
181
|
+
# Initialize the transport
|
182
|
+
self.transport = self.transport_class.new(self.options)
|
183
|
+
|
184
|
+
# Setup transport callbacks
|
185
|
+
self.transport.on(:open) do
|
186
|
+
puts "TRANSPORT OPEN" if self.verbose
|
187
|
+
|
188
|
+
# Call the callback
|
189
|
+
@on_connect.call if @on_connect
|
190
|
+
|
191
|
+
# Create the session
|
192
|
+
self._create_session
|
193
|
+
|
194
|
+
end
|
195
|
+
|
196
|
+
self.transport.on(:close) do |reason|
|
197
|
+
puts "TRANSPORT CLOSED: #{reason}" if self.verbose
|
198
|
+
@open = false
|
199
|
+
|
200
|
+
unless @retrying
|
201
|
+
@on_disconnect.call(reason) if @on_disconnect
|
202
|
+
end
|
203
|
+
|
204
|
+
# Nil out the session since the transport closed underneath it
|
205
|
+
self.session = nil
|
206
|
+
|
207
|
+
if @reconnect
|
208
|
+
# Retry
|
209
|
+
self._retry unless @retrying
|
210
|
+
else
|
211
|
+
# Stop the Event Machine
|
212
|
+
self.transport_class.stop_event_machine
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
self.transport.on(:error) do |message|
|
217
|
+
puts "TRANSPORT ERROR: #{message}"
|
218
|
+
end
|
219
|
+
|
220
|
+
@open = true
|
221
|
+
|
222
|
+
self.transport.connect
|
223
|
+
|
224
|
+
end
|
225
|
+
|
226
|
+
def _finish_retry
|
227
|
+
@retry_timer = 1
|
228
|
+
@retrying = false
|
229
|
+
end
|
230
|
+
|
231
|
+
def _retry
|
232
|
+
|
233
|
+
if self.session == nil or not self.session.is_open?
|
234
|
+
@retry_timer = 2*@retry_timer unless @retry_timer == 32
|
235
|
+
@retrying = true
|
236
|
+
|
237
|
+
self._create_transport
|
238
|
+
|
239
|
+
puts "Attempting Reconnect... Next attempt in #{@retry_timer} seconds" if self.verbose
|
240
|
+
self.transport_class.add_timer(@retry_timer*1000) do
|
241
|
+
self._retry if @retrying
|
242
|
+
end
|
243
|
+
end
|
244
|
+
|
245
|
+
end
|
246
|
+
|
247
|
+
end
|
248
|
+
end
|
249
|
+
end
|