Class: XRBP::WebSocket::Plugins::ConnectionTimeout
- Inherits:
-
PluginBase
- Object
- PluginBase
- XRBP::WebSocket::Plugins::ConnectionTimeout
- Defined in:
- lib/xrbp/websocket/plugins/connection_timeout.rb
Overview
Automatic disconnection if no server data in certain time
Constant Summary collapse
- DEFAULT_TIMEOUT =
10
Instance Attribute Summary collapse
-
#connection_timeout ⇒ Object
Returns the value of attribute connection_timeout.
Attributes inherited from PluginBase
Instance Method Summary collapse
- #added ⇒ Object
- #closed ⇒ Object
-
#initialize(connection) ⇒ ConnectionTimeout
constructor
A new instance of ConnectionTimeout.
- #message(msg) ⇒ Object
- #opened ⇒ Object
- #timeout? ⇒ Boolean
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_timeout ⇒ Object
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
#added ⇒ Object
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 |
#closed ⇒ Object
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 (msg) @last_msg = Time.now end |
#opened ⇒ Object
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
25 26 27 |
# File 'lib/xrbp/websocket/plugins/connection_timeout.rb', line 25 def timeout? Time.now - @last_msg > @connection_timeout end |