Class: XRBP::WebSocket::Plugins::ConnectionTimeout

Inherits:
PluginBase
  • Object
show all
Defined in:
lib/xrbp/websocket/plugins/connection_timeout.rb

Overview

Automatic disconnection if no server data in certain time

Examples:

timed out connection

connection = WebSocket::Connection.new "wss://s1.ripple.com:443"
connection.add_plugin :connection_timeout
connection.connection_timeout = 3
connection.connect
sleep(3)
connection.closed? # => true

Constant Summary collapse

DEFAULT_TIMEOUT =
10

Instance Attribute Summary collapse

Attributes inherited from PluginBase

#connection

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ ConnectionTimeout

Returns a new instance of ConnectionTimeout.


20
21
22
23
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 20

def initialize(connection)
  super(connection)
  @connection_timeout = DEFAULT_TIMEOUT
end

Instance Attribute Details

#connection_timeoutObject

Returns the value of attribute connection_timeout


16
17
18
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 16

def connection_timeout
  @connection_timeout
end

Instance Method Details

#addedObject


29
30
31
32
33
34
35
36
37
38
39
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 29

def added
  plugin = self
  connection.define_instance_method(:connection_timeout=) do |t|
    plugin.connection_timeout = t

    connections.each{ |c|
      c.plugin(ConnectionTimeout)
       .connection_timeout = t
    } if self.kind_of?(MultiConnection)
  end
end

#closedObject


57
58
59
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 57

def closed
  terminate!
end

#message(msg) ⇒ Object


41
42
43
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 41

def message(msg)
  @last_msg = Time.now
end

#openedObject


45
46
47
48
49
50
51
52
53
54
55
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 45

def opened
  connection.add_work do
    @last_msg = Time.now
    until terminate?             ||
          connection.force_quit? ||
          connection.closed?
      connection.async_close! if timeout?
      connection.rsleep(0.1)
    end
  end
end

#timeout?Boolean

Returns:

  • (Boolean)

25
26
27
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 25

def timeout?
  Time.now - @last_msg > @connection_timeout
end