···11+module Skyfall
22+33+ # @private
44+ module Events
55+ protected
66+77+ def event_handler(name)
88+ define_method("on_#{name}") do |&block|
99+ @handlers[name.to_sym] = block
1010+ end
1111+1212+ define_method("on_#{name}=") do |block|
1313+ @handlers[name.to_sym] = block
1414+ end
1515+ end
1616+ end
1717+end
+79-9
lib/skyfall/stream.rb
···22require 'faye/websocket'
33require 'uri'
4455+require_relative 'events'
56require_relative 'version'
6778module Skyfall
···1415 # custom client for a websocket API that isn't supported yet.
15161617 class Stream
1717- EVENTS = %w(message raw_message connecting connect disconnect reconnect error timeout)
1818+ extend Events
1919+1820 MAX_RECONNECT_INTERVAL = 300
19212022 # If enabled, the client will try to reconnect if the connection is closed unexpectedly.
···209211 end
210212 end
211213212212- EVENTS.each do |event|
213213- define_method "on_#{event}" do |&block|
214214- @handlers[event.to_sym] = block
215215- end
214214+215215+ # @!method on_connecting(block)
216216+ # Defines a callback to be run when the client tries to open a connection to the websocket.
217217+ # Can be also run as a setter `on_connecting=`.
218218+ # @param [Proc] block
219219+ # @yieldparam [String] url URL to which the client is connecting
220220+ # @return [nil]
221221+222222+ event_handler :connecting
216223217217- define_method "on_#{event}=" do |block|
218218- @handlers[event.to_sym] = block
219219- end
220220- end
224224+ # @!method on_connect(block)
225225+ # Defines a callback to be run after a connection to the websocket is opened.
226226+ # Can be also run as a setter `on_connect=`.
227227+ # @param [Proc] block
228228+ # @return [nil]
229229+230230+ event_handler :connect
231231+232232+ # @!method on_raw_message(block)
233233+ # Defines a callback to be run when a message is received, passing a raw data packet as
234234+ # received from the websocket (plain text or binary). Can be also run as a setter `on_raw_message=`.
235235+ # @param [Proc] block
236236+ # @yieldparam [String] data payload of the received message
237237+ # @return [nil]
238238+239239+ event_handler :raw_message
240240+241241+ # @!method on_message(block)
242242+ # Defines a callback to be run when a message is received, passing the message as a parsed
243243+ # object of an appropriate message class. Can be also run as a setter `on_message=`.
244244+ # @param [Proc] block
245245+ # @yieldparam [Object] message parsed message of an appropriate class
246246+ # @return [nil]
247247+248248+ event_handler :message
249249+250250+ # @!method on_disconnect(block)
251251+ # Defines a callback to be run after a connection to the websocket is closed (and the client
252252+ # does not reconnect). Can be also run as a setter `on_disconnect=`.
253253+ #
254254+ # This callback is not run when `on_reconnect` fires.
255255+ # @param [Proc] block
256256+ # @return [nil]
257257+258258+ event_handler :disconnect
259259+260260+ # @!method on_reconnect(block)
261261+ # Defines a callback to be run when a connection to the websocket is broken, but the client
262262+ # initiates or schedules a reconnect (which may happen after a delay). Can be also run as
263263+ # a setter `on_reconnect=`.
264264+ # @param [Proc] block
265265+ # @return [nil]
266266+267267+ event_handler :reconnect
268268+269269+ # @!method on_timeout(block)
270270+ # Defines a callback to be run when the heartbeat timer forces a reconnect. A reconnect is
271271+ # triggered after not receiving any messages for a period of time specified in {#heartbeat_timeout}
272272+ # (if {#check_heartbeat} is enabled). Can be also run as a setter `on_timeout=`.
273273+ #
274274+ # This callback is also followed by `on_reconnect`.
275275+ # @param [Proc] block
276276+ # @return [nil]
277277+278278+ event_handler :timeout
279279+280280+ # @!method on_error(block)
281281+ # Defines a callback to be run when the websocket connection returns an error. Can be also
282282+ # run as a setter `on_error=`.
283283+ #
284284+ # Default handler prints the error to stdout.
285285+ #
286286+ # @param [Proc] block
287287+ # @yieldparam [Exception] error the received error
288288+ # @return [nil]
289289+290290+ event_handler :error
221291222292223293 # Returns a string with a representation of the object for debugging purposes.