Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2.0.0
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2016 Eric Chapman
Copyright (c) 2018 Eric Chapman

MIT License

Expand Down
70 changes: 56 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Please use [wamp_rails](https://github.com/ericchapman/ruby_wamp_rails) to integ

## Revision History

- v0.0.9:
- Added support for transport override and 'faye-websocket' transport
- Added "on(event)" callback (still support legacy methods)
- Increased Test Coverage for 'Transport' and 'Connection' classes
- v0.0.8:
- Exposed 'yield' publicly to allow higher level libraries to not use the 'defer'
- Removed library version dependency
Expand Down Expand Up @@ -70,7 +74,7 @@ options = {
}
connection = WampClient::Connection.new(options)

connection.on_join do |session, details|
connection.on(:join) do |session, details|
puts "Session Open"

# Register for something
Expand Down Expand Up @@ -101,46 +105,84 @@ A connection is closed by simply calling "close"
connection.close
```

Note that the connection will still call "on_leave" and "on_disconnect" as it closes the session and the transport
Note that the connection will still call "on(:leave)" and "on(:disconnect)" as it closes the session and the transport

#### Callbacks
A connection has the following callbacks

**on_connect** - Called when the transport is opened
**on(:connect)** - Called when the transport is opened
```ruby
connection.on_connect do
connection.on(:connect) do

end
```

**on_join** - Called when the session is established
**on(:join)** - Called when the session is established
```ruby
connection.on_join do |session, details|
connection.on(:join) do |session, details|

end
```

**on_leave** - Called when the session is terminated
**on(:leave)** - Called when the session is terminated
```ruby
connection.on_leave do |reason, details|
connection.on(:leave) do |reason, details|

end
```

**on_disconnect** - Called when the connection is terminated
**on(:disconnect)** - Called when the connection is terminated
```ruby
connection.on_disconnect do |reason|
connection.on(:disconnect) do |reason|

end
```

**on_challenge** - Called when an authentication challenge is created
**on(:challenge)** - Called when an authentication challenge is created
```ruby
connection.on_challenge do |authmethod, extra|
connection.on(:challenge) do |authmethod, extra|

end
```

#### Overriding Transport
By default, the library will use the "websocket-eventmachine-client" Gem as the websocket transport.
However the library also supports overriding this.

##### GEM: faye-websocket
To use this library, do the following

Install the "faye-websocket" Gem:

$ gem install faye-websocket

Override the transport by doing the following:

```ruby
require 'wamp_client'

options = {
uri: 'ws://127.0.0.1:8080/ws',
realm: 'realm1',
proxy: { # See faye-websocket documentation
:origin => 'http://username:password@proxy.example.com',
:headers => {'User-Agent' => 'ruby'}
},
transport: WampClient::Transport::FayeWebSocket
}

connection = WampClient::Connection.new(options)

# More code
```

Note that the "faye-wesbsocket" transport supports passing in a "proxy" as shown above.

##### Custom
You can also create your own transports by wrapping them in a "Transport" object
and including as shown above. For more details on this, see the files in
"lib/wamp_client/transport"

### Authentication
The library supports authentication. Here is how to perform the different methods

Expand All @@ -158,7 +200,7 @@ options = {
}
connection = WampClient::Connection.new(options)

connection.on_challenge do |authmethod, extra|
connection.on(:challenge) do |authmethod, extra|
puts 'Challenge'
if authmethod == 'wampcra'
WampClient::Auth::Cra.sign('secret', extra[:challenge])
Expand All @@ -167,7 +209,7 @@ connection.on_challenge do |authmethod, extra|
end
end

connection.on_join do |session, details|
connection.on(:join) do |session, details|
puts "Session Open"
end

Expand Down
3 changes: 1 addition & 2 deletions lib/wamp_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
require 'wamp_client/version'
require 'wamp_client/message'
require 'wamp_client/serializer'
require 'wamp_client/transport'
require 'wamp_client/connection'
require 'wamp_client/session'
require 'wamp_client/auth'
require 'wamp_client/defer'
require 'wamp_client/defer'
62 changes: 43 additions & 19 deletions lib/wamp_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@

=end

require 'websocket-eventmachine-client'
require 'wamp_client/session'
require 'wamp_client/transport'
require 'wamp_client/transport/web_socket_event_machine'
require 'wamp_client/transport/faye_web_socket'

module WampClient
class Connection
attr_accessor :options, :transport, :session
attr_accessor :options, :transport_class, :transport, :session, :verbose

@reconnect = true

Expand Down Expand Up @@ -77,16 +77,37 @@ def on_disconnect(&on_disconnect)
@on_disconnect = on_disconnect
end

# Simple setter for callbacks
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

# @param options [Hash] The different options to pass to the connection
# @option options [String] :uri The uri of the WAMP router to connect to
# @option options [String] :proxy The proxy to get to the router
# @option options [String] :realm The realm to connect to
# @option options [String,nil] :protocol The protocol (default if wamp.2.json)
# @option options [String,nil] :authid The id to authenticate with
# @option options [Array, nil] :authmethods The different auth methods that the client supports
# @option options [Hash] :headers Custom headers to include during the connection
# @option options [WampClient::Serializer::Base] :serializer The serializer to use (default is json)
def initialize(options)
self.transport_class = options.delete(:transport) || WampClient::Transport::WebSocketEventMachine
self.options = options || {}
self.verbose = options[:verbose] || false
end

# Opens the connection
Expand All @@ -98,7 +119,7 @@ def open
@retry_timer = 1
@retrying = false

EM.run do
self.transport_class.start_event_machine do
# Create the transport
self._create_transport
end
Expand All @@ -112,6 +133,7 @@ def close

# Leave the session
@reconnect = false
@retrying = false
session.leave

end
Expand All @@ -120,17 +142,17 @@ def _create_session
self.session = WampClient::Session.new(self.transport, self.options)

# Setup session callbacks
self.session.on_challenge do |authmethod, extra|
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.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|
self.session.on(:leave) do |reason, details|

unless @retrying
@on_leave.call(reason, details) if @on_leave
Expand All @@ -156,14 +178,11 @@ def _create_transport
end

# Initialize the transport
if self.options[:transport]
self.transport = self.options[:transport]
else
self.transport = WampClient::Transport::WebSocketTransport.new(self.options)
end
self.transport = self.transport_class.new(self.options)

# Setup transport callbacks
self.transport.on_open do
self.transport.on(:open) do
puts "TRANSPORT OPEN" if self.verbose

# Call the callback
@on_connect.call if @on_connect
Expand All @@ -173,7 +192,8 @@ def _create_transport

end

self.transport.on_close do |reason|
self.transport.on(:close) do |reason|
puts "TRANSPORT CLOSED: #{reason}" if self.verbose
@open = false

unless @retrying
Expand All @@ -188,10 +208,14 @@ def _create_transport
self._retry unless @retrying
else
# Stop the Event Machine
EM.stop
self.transport_class.stop_event_machine
end
end

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

@open = true

self.transport.connect
Expand All @@ -205,16 +229,16 @@ def _finish_retry

def _retry

unless self.session and self.session.is_open?
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"
EM.add_timer(@retry_timer) {
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
Expand Down
19 changes: 17 additions & 2 deletions lib/wamp_client/session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

=end

require 'wamp_client/transport'
require 'wamp_client/transport/base'
require 'wamp_client/message'
require 'wamp_client/check'
require 'wamp_client/version'
Expand Down Expand Up @@ -167,6 +167,20 @@ def on_challenge(&on_challenge)
@on_challenge = on_challenge
end

# Simple setter for callbacks
def on(event, &callback)
case event
when :join
self.on_join(&callback)
when :challenge
self.on_challenge(&callback)
when :leave
self.on_leave(&callback)
else
raise RuntimeError, "Unknown on(event) '#{event}'"
end
end

attr_accessor :id, :realm, :transport, :verbose, :options

# Private attributes
Expand Down Expand Up @@ -212,6 +226,7 @@ def initialize(transport, options={})
# Setup session callbacks
@on_join = nil
@on_leave = nil
@on_challenge = nil

end

Expand Down Expand Up @@ -908,7 +923,7 @@ def call(procedure, args=nil, kwargs=nil, options={}, &callback)

# Timeout Logic
if options[:timeout] and options[:timeout] > 0
self.transport.timer(options[:timeout]) do
self.transport.add_timer(options[:timeout]) do
# Once the timer expires, if the call hasn't completed, cancel it
if self._requests[:call][call.id]
call.cancel
Expand Down
Loading