Class: XRBP::WebSocket::Plugins::AutoConnect

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

Overview

Automatically connects on instantiation and reconnects on close events.

Examples:

autoconnecting

connection = WebSocket::Connection.new "wss://s1.ripple.com:443"
connection.add_plugin :autoconnect
connection.open? # => true

Instance Attribute Summary collapse

Attributes inherited from PluginBase

#connection

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ AutoConnect

Returns a new instance of AutoConnect.


14
15
16
17
# File 'lib/xrbp/websocket/plugins/autoconnect.rb', line 14

def initialize(connection)
  super(connection)
  @reconnect_delay = nil
end

Instance Attribute Details

#reconnect_delayObject

Returns the value of attribute reconnect_delay.


12
13
14
# File 'lib/xrbp/websocket/plugins/autoconnect.rb', line 12

def reconnect_delay
  @reconnect_delay
end

Instance Method Details

#addedObject


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/xrbp/websocket/plugins/autoconnect.rb', line 19

def added
  plugin = self

  connection.define_instance_method(:reconnect_delay=) do |d|
    plugin.reconnect_delay = d

    connections.each{ |c|
      c.plugin(AutoConnect)
       .reconnect_delay = d
    } if self.kind_of?(MultiConnection)
  end

  return if connection.kind_of?(MultiConnection)

  conn = connection
  connection.on :completed do
    connected = false
    until conn.force_quit? || connected
      conn.rsleep(plugin.reconnect_delay) if plugin.reconnect_delay
      next if conn.force_quit?

      begin
        conn.connect
        connected = true
      rescue
        conn.rsleep(3)
      end
    end
  end

  until connection.force_quit? || connection.open?
    begin
      connection.connect
    rescue
      connection.rsleep(3)
    end
  end
end