Class: Skyfall::Stream
- Inherits:
-
Object
- Object
- Skyfall::Stream
- Defined in:
- lib/skyfall/stream.rb
Constant Summary collapse
- EVENTS =
%w(message raw_message connecting connect disconnect reconnect error timeout)
- MAX_RECONNECT_INTERVAL =
300
Instance Attribute Summary collapse
-
#auto_reconnect ⇒ Object
Returns the value of attribute auto_reconnect.
-
#check_heartbeat ⇒ Object
Returns the value of attribute check_heartbeat.
-
#heartbeat_interval ⇒ Object
Returns the value of attribute heartbeat_interval.
-
#heartbeat_timeout ⇒ Object
Returns the value of attribute heartbeat_timeout.
-
#last_update ⇒ Object
Returns the value of attribute last_update.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
Class Method Summary collapse
Instance Method Summary collapse
- #connect ⇒ Object
- #default_user_agent ⇒ Object
- #disconnect ⇒ Object (also: #close)
- #handle_message(msg) ⇒ Object
-
#initialize(service) ⇒ Stream
constructor
A new instance of Stream.
- #inspect ⇒ Object
- #inspectable_variables ⇒ Object
- #reconnect ⇒ Object
- #start_heartbeat_timer ⇒ Object
- #stop_heartbeat_timer ⇒ Object
- #version_string ⇒ Object
Constructor Details
#initialize(service) ⇒ Stream
Returns a new instance of Stream.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/skyfall/stream.rb', line 25 def initialize(service) @root_url = build_root_url(service) @handlers = {} @auto_reconnect = true @check_heartbeat = false @connection_attempts = 0 @heartbeat_interval = 10 @heartbeat_timeout = 300 @last_update = nil @user_agent = default_user_agent @handlers[:error] = proc { |e| puts "ERROR: #{e}" } end |
Instance Attribute Details
#auto_reconnect ⇒ Object
Returns the value of attribute auto_reconnect.
10 11 12 |
# File 'lib/skyfall/stream.rb', line 10 def auto_reconnect @auto_reconnect end |
#check_heartbeat ⇒ Object
Returns the value of attribute check_heartbeat.
11 12 13 |
# File 'lib/skyfall/stream.rb', line 11 def check_heartbeat @check_heartbeat end |
#heartbeat_interval ⇒ Object
Returns the value of attribute heartbeat_interval.
11 12 13 |
# File 'lib/skyfall/stream.rb', line 11 def heartbeat_interval @heartbeat_interval end |
#heartbeat_timeout ⇒ Object
Returns the value of attribute heartbeat_timeout.
11 12 13 |
# File 'lib/skyfall/stream.rb', line 11 def heartbeat_timeout @heartbeat_timeout end |
#last_update ⇒ Object
Returns the value of attribute last_update.
10 11 12 |
# File 'lib/skyfall/stream.rb', line 10 def last_update @last_update end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
10 11 12 |
# File 'lib/skyfall/stream.rb', line 10 def user_agent @user_agent end |
Class Method Details
.new(server, endpoint = nil, cursor = nil) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/skyfall/stream.rb', line 13 def self.new(server, endpoint = nil, cursor = nil) # to be removed in 0.6 if endpoint || cursor STDERR.puts "Warning: Skyfall::Stream has been renamed to Skyfall::Firehose. This initializer will be removed in the next version." Firehose.new(server, endpoint, cursor) else instance = self.allocate instance.send(:initialize, server) instance end end |
Instance Method Details
#connect ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/skyfall/stream.rb', line 40 def connect return if @ws url = build_websocket_url @handlers[:connecting]&.call(url) @engines_on = true @reconnect_timer&.cancel @reconnect_timer = nil EM.run do EventMachine.error_handler do |e| @handlers[:error]&.call(e) end @ws = Faye::WebSocket::Client.new(url, nil, { headers: { 'User-Agent' => user_agent }}) @ws.on(:open) do |e| @handlers[:connect]&.call @last_update = Time.now start_heartbeat_timer end @ws.on(:message) do |msg| @reconnecting = false @connection_attempts = 0 @last_update = Time.now (msg) end @ws.on(:error) do |e| @handlers[:error]&.call(e) end @ws.on(:close) do |e| @ws = nil if @reconnecting || @auto_reconnect && @engines_on @handlers[:reconnect]&.call @reconnect_timer&.cancel @reconnect_timer = EM::Timer.new(reconnect_delay) do @connection_attempts += 1 connect end else stop_heartbeat_timer @engines_on = false @handlers[:disconnect]&.call EM.stop_event_loop unless @ws end end end end |
#default_user_agent ⇒ Object
118 119 120 |
# File 'lib/skyfall/stream.rb', line 118 def default_user_agent version_string end |
#disconnect ⇒ Object Also known as: close
108 109 110 111 112 113 114 |
# File 'lib/skyfall/stream.rb', line 108 def disconnect return unless EM.reactor_running? @reconnecting = false @engines_on = false EM.stop_event_loop end |
#handle_message(msg) ⇒ Object
96 97 98 99 |
# File 'lib/skyfall/stream.rb', line 96 def (msg) data = msg.data @handlers[:raw_message]&.call(data) end |
#inspect ⇒ Object
170 171 172 173 |
# File 'lib/skyfall/stream.rb', line 170 def inspect vars = inspectable_variables.map { |v| "#{v}=#{instance_variable_get(v).inspect}" }.join(", ") "#<#{self.class}:0x#{object_id} #{vars}>" end |
#inspectable_variables ⇒ Object
166 167 168 |
# File 'lib/skyfall/stream.rb', line 166 def inspectable_variables instance_variables - [:@handlers, :@ws] end |
#reconnect ⇒ Object
101 102 103 104 105 106 |
# File 'lib/skyfall/stream.rb', line 101 def reconnect @reconnecting = true @connection_attempts = 0 @ws ? @ws.close : connect end |
#start_heartbeat_timer ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/skyfall/stream.rb', line 136 def start_heartbeat_timer return if !@check_heartbeat || @heartbeat_interval.to_f <= 0 || @heartbeat_timeout.to_f <= 0 return if @heartbeat_timer @heartbeat_timer = EM::PeriodicTimer.new(@heartbeat_interval) do next if @ws.nil? || @heartbeat_timeout.to_f <= 0 time_passed = Time.now - @last_update if time_passed > @heartbeat_timeout @handlers[:timeout]&.call reconnect end end end |
#stop_heartbeat_timer ⇒ Object
151 152 153 154 |
# File 'lib/skyfall/stream.rb', line 151 def stop_heartbeat_timer @heartbeat_timer&.cancel @heartbeat_timer = nil end |