Class: Cybertrain::WebSocket::Subscription

Inherits:
Object
  • Object
show all
Defined in:
cybertrain/websocket.rb

Overview

One registered feed, from subscribe. Distinctive accessor names and no #call (NOTES rules 10/34/40).

Instance Method Summary collapse

Instance Method Details

#on_message(&block) ⇒ Subscription

Sets the block called with each message: |conn, data|, a Connection and the bytes (a text message is UTF-8 as the server sent it, not checked). Required before Cybertrain::WebSocket.start!.

Returns:



231
232
233
234
# File 'cybertrain/websocket.rb', line 231

def on_message(&block)
  @sub_message_block = block
  self
end

#on_open(&block) ⇒ Subscription

Sets the block called after each (re)connect, before any message: send the subscription request here. It receives a Connection.

Returns:



221
222
223
224
# File 'cybertrain/websocket.rb', line 221

def on_open(&block)
  @sub_open_block = block
  self
end

#on_tick(seconds, &block) ⇒ Subscription

Sets the block called every seconds on the feed’s own thread, between its one-second waits, whether or not the feed is connected: |conn|, a Connection while connected and nil while not (test it). The first call is seconds after the feed starts. An exception is logged and the feed goes on, as for on_message. Work that belongs to the feed’s thread (flushing what on_message collected, closing a time bucket) goes here, with no lock and no scheduler task.

Parameters:

  • seconds (Integer) —

    at least 1 (the granularity is the one-second receive timeout)

Returns:

Raises:

  • (ArgumentError) —

    seconds is not an Integer of at least 1, or the subscription already has an on_tick



250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'cybertrain/websocket.rb', line 250

def on_tick(seconds, &block)
  unless seconds.is_a?(Integer) && seconds >= 1
    message = "WebSocket subscription #{@sub_name.inspect}: on_tick needs whole seconds, at least 1 (got #{seconds.inspect})"
    raise ArgumentError, message
  end
  unless @sub_tick_block.nil?
    message = "WebSocket subscription #{@sub_name.inspect} already has on_tick"
    raise ArgumentError, message
  end
  @sub_tick_block = block
  @sub_tick_seconds = seconds
  self
end

#sub_name ⇒ String

Returns the name given to Cybertrain::WebSocket.subscribe.

Returns:



213
214
215
# File 'cybertrain/websocket.rb', line 213

def sub_name
  @sub_name
end