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

Class Method Summary collapse

Instance Method Summary collapse

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_reconnectObject

Returns the value of attribute auto_reconnect.



10
11
12
# File 'lib/skyfall/stream.rb', line 10

def auto_reconnect
  @auto_reconnect
end

#check_heartbeatObject

Returns the value of attribute check_heartbeat.



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

def check_heartbeat
  @check_heartbeat
end

#heartbeat_intervalObject

Returns the value of attribute heartbeat_interval.



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

def heartbeat_interval
  @heartbeat_interval
end

#heartbeat_timeoutObject

Returns the value of attribute heartbeat_timeout.



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

def heartbeat_timeout
  @heartbeat_timeout
end

#last_updateObject

Returns the value of attribute last_update.



10
11
12
# File 'lib/skyfall/stream.rb', line 10

def last_update
  @last_update
end

#user_agentObject

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

#connectObject



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
      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



118
119
120
# File 'lib/skyfall/stream.rb', line 118

def default_user_agent
  version_string
end

#disconnectObject 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 handle_message(msg)
  data = msg.data
  @handlers[:raw_message]&.call(data)
end

#inspectObject



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_variablesObject



166
167
168
# File 'lib/skyfall/stream.rb', line 166

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

#reconnectObject



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_timerObject



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_timerObject



151
152
153
154
# File 'lib/skyfall/stream.rb', line 151

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

#version_stringObject



122
123
124
# File 'lib/skyfall/stream.rb', line 122

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