Class: Skyfall::Stream

Inherits:
Object
  • Object
show all
Defined in:
lib/skyfall/stream.rb

Direct Known Subclasses

Firehose, Jetstream

Constant Summary collapse

EVENTS =
%w(message raw_message connecting connect disconnect reconnect error timeout)
MAX_RECONNECT_INTERVAL =
300

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service) ⇒ Stream

Returns a new instance of Stream.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/skyfall/stream.rb', line 15

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_reconnectObject

Returns the value of attribute auto_reconnect.



12
13
14
# File 'lib/skyfall/stream.rb', line 12

def auto_reconnect
  @auto_reconnect
end

#check_heartbeatObject

Returns the value of attribute check_heartbeat.



13
14
15
# File 'lib/skyfall/stream.rb', line 13

def check_heartbeat
  @check_heartbeat
end

#heartbeat_intervalObject

Returns the value of attribute heartbeat_interval.



13
14
15
# File 'lib/skyfall/stream.rb', line 13

def heartbeat_interval
  @heartbeat_interval
end

#heartbeat_timeoutObject

Returns the value of attribute heartbeat_timeout.



13
14
15
# File 'lib/skyfall/stream.rb', line 13

def heartbeat_timeout
  @heartbeat_timeout
end

#last_updateObject

Returns the value of attribute last_update.



12
13
14
# File 'lib/skyfall/stream.rb', line 12

def last_update
  @last_update
end

#user_agentObject

Returns the value of attribute user_agent.



12
13
14
# File 'lib/skyfall/stream.rb', line 12

def user_agent
  @user_agent
end

Instance Method Details

#connectObject



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
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
# File 'lib/skyfall/stream.rb', line 30

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 = build_websocket_client(url)

    @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
      handle_message(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_agentObject



108
109
110
# File 'lib/skyfall/stream.rb', line 108

def default_user_agent
  version_string
end

#disconnectObject Also known as: close



98
99
100
101
102
103
104
# File 'lib/skyfall/stream.rb', line 98

def disconnect
  return unless EM.reactor_running?

  @reconnecting = false
  @engines_on = false
  EM.stop_event_loop
end

#handle_message(msg) ⇒ Object



86
87
88
89
# File 'lib/skyfall/stream.rb', line 86

def handle_message(msg)
  data = msg.data
  @handlers[:raw_message]&.call(data)
end

#inspectObject



160
161
162
163
# File 'lib/skyfall/stream.rb', line 160

def inspect
  vars = inspectable_variables.map { |v| "#{v}=#{instance_variable_get(v).inspect}" }.join(", ")
  "#<#{self.class}:0x#{object_id} #{vars}>"
end

#inspectable_variablesObject



156
157
158
# File 'lib/skyfall/stream.rb', line 156

def inspectable_variables
  instance_variables - [:@handlers, :@ws]
end

#reconnectObject



91
92
93
94
95
96
# File 'lib/skyfall/stream.rb', line 91

def reconnect
  @reconnecting = true
  @connection_attempts = 0

  @ws ? @ws.close : connect
end

#start_heartbeat_timerObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/skyfall/stream.rb', line 126

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_timerObject



141
142
143
144
# File 'lib/skyfall/stream.rb', line 141

def stop_heartbeat_timer
  @heartbeat_timer&.cancel
  @heartbeat_timer = nil
end

#version_stringObject



112
113
114
# File 'lib/skyfall/stream.rb', line 112

def version_string
  "Skyfall/#{Skyfall::VERSION}"
end