Method: OpenC3::WebSocketApi#read

Defined in:
lib/openc3/script/web_socket_api.rb

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

Read the next message with json parsing, filtering, and timeout support



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
# File 'lib/openc3/script/web_socket_api.rb', line 54

def read(ignore_protocol_messages: true, timeout: nil)
  start_time = Time.now
  while true
    message = read_message()
    if message
      json_hash = JSON.parse(message, allow_nan: true, create_additions: true)
      if ignore_protocol_messages
        type = json_hash['type']
        if type # ping, welcome, confirm_subscription, reject_subscription, disconnect
          if type == 'disconnect'
            if json_hash['reason'] == 'unauthorized'
              raise "Unauthorized"
            end
          end
          if type == 'reject_subscription'
            raise "Subscription Rejected"
          end
          if timeout
            end_time = Time.now
            if (start_time - end_time) > timeout
              raise Timeout::Error, "No Data Timeout"
            end
          end
          if defined? RunningScript and RunningScript.instance
            raise StopScript if RunningScript.instance.stop?
          end
          next
        end
      end
      return json_hash['message']
    end
    return message
  end
end