Class: OpenC3::RunningScriptWebSocketApi

Inherits:
ScriptWebSocketApi show all
Defined in:
lib/openc3/script/web_socket_api.rb

Overview

Running Script WebSocket

Constant Summary

Constants inherited from WebSocketApi

WebSocketApi::DEFAULT_OPTIONS, WebSocketApi::USER_AGENT

Instance Method Summary collapse

Methods inherited from ScriptWebSocketApi

#generate_url

Methods inherited from WebSocketApi

cable_url, #check_protocol_frame, #connected?, #generate_auth, #parse_message, #read_message, #wait_for_subscribed, #write, #write_action, #write_command

Constructor Details

#initialize(id:, **options) ⇒ RunningScriptWebSocketApi

Returns a new instance of RunningScriptWebSocketApi.



309
310
311
312
313
314
315
316
# File 'lib/openc3/script/web_socket_api.rb', line 309

def initialize(id:, **options)
  @pending_events = []
  @identifier = {
    channel: "RunningScriptChannel",
    id: id
  }
  super(**options)
end

Instance Method Details

#connectObject



345
346
347
348
# File 'lib/openc3/script/web_socket_api.rb', line 345

def connect
  @pending_events.clear
  super
end

#disconnectObject



355
356
357
358
# File 'lib/openc3/script/web_socket_api.rb', line 355

def disconnect
  @pending_events.clear
  super
end

#read(ignore_protocol_messages: true, timeout: nil) ⇒ Object

RunningScriptChannel batches events to reduce ActionCable frame and client-dispatch overhead. Preserve the public API's historical contract: callers still receive one script event from each read.



333
334
335
336
337
338
339
340
341
342
343
# File 'lib/openc3/script/web_socket_api.rb', line 333

def read(ignore_protocol_messages: true, timeout: nil)
  return @pending_events.shift unless @pending_events.empty?

  loop do
    message = super
    return message unless message.is_a?(Array)

    @pending_events.concat(message)
    return @pending_events.shift unless @pending_events.empty?
  end
end

#subscribeObject

The backlog of script events is transmitted with the subscription confirmation, but LIVE events only flow once the client reports that it is ready to stream events (see RunningScriptChannel#ready) -- a broadcast sent before the gateway has registered this subscription's stream would be silently dropped. subscribe() blocks until confirm_subscription, so the 'ready' action is guaranteed to arrive after the stream is registered.



324
325
326
327
328
# File 'lib/openc3/script/web_socket_api.rb', line 324

def subscribe
  was_subscribed = @subscribed
  super
  write_action({ 'action' => 'ready' }) unless was_subscribed
end

#unsubscribeObject



350
351
352
353
# File 'lib/openc3/script/web_socket_api.rb', line 350

def unsubscribe
  @pending_events.clear
  super
end