Class: Cybertrain::WebSocket::Subscription
- Inherits:
-
Object
- Object
- Cybertrain::WebSocket::Subscription
- 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
-
#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). -
#on_open(&block) ⇒ Subscription
Sets the block called after each (re)connect, before any message: send the subscription request here.
-
#on_tick(seconds, &block) ⇒ Subscription
Sets the block called every
secondson 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). -
#sub_name ⇒ String
The name given to subscribe.
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!.
231 232 233 234 |
# File 'cybertrain/websocket.rb', line 231 def (&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.
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.
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 = "WebSocket subscription #{@sub_name.inspect}: on_tick needs whole seconds, at least 1 (got #{seconds.inspect})" raise ArgumentError, end unless @sub_tick_block.nil? = "WebSocket subscription #{@sub_name.inspect} already has on_tick" raise ArgumentError, end @sub_tick_block = block @sub_tick_seconds = seconds self end |
#sub_name ⇒ String
Returns the name given to Cybertrain::WebSocket.subscribe.
213 214 215 |
# File 'cybertrain/websocket.rb', line 213 def sub_name @sub_name end |