Class: RubyAsterisk::ARI::WebSocket
- Inherits:
-
Object
- Object
- RubyAsterisk::ARI::WebSocket
- Includes:
- Connection, EventHandlers, Heartbeat, Reconnect
- Defined in:
- lib/ruby-asterisk/ari/websocket.rb,
lib/ruby-asterisk/ari/websocket/heartbeat.rb,
lib/ruby-asterisk/ari/websocket/reconnect.rb,
lib/ruby-asterisk/ari/websocket/connection.rb,
lib/ruby-asterisk/ari/websocket/event_handlers.rb,
lib/ruby-asterisk/ari/websocket/socket_adapter.rb
Overview
WebSocket client for ARI events Connects to the Asterisk ARI WebSocket endpoint to receive real-time events
I/O model (no EventMachine): the WebSocket protocol is handled by the pure Ruby websocket-driver gem over a plain TCPSocket (SSLSocket for wss).
connection_thread — owns the socket lifecycle: connects, runs the read
loop (readpartial -> driver.parse -> callbacks), and
re-connects after a drop while auto-reconnect is on.
ping_thread — started when the connection opens; wakes every
ping_interval seconds and sends a WebSocket ping.
Event callbacks execute in the connection thread. All driver calls are serialized through a reentrant Monitor so send_message? is safe from any thread.
Defined Under Namespace
Modules: Connection, EventHandlers, Heartbeat, Reconnect Classes: SocketAdapter
Constant Summary collapse
- PING_INTERVAL =
Ping interval in seconds to keep connection alive
30- RECONNECT_DELAY =
Reconnect delay in seconds (base for exponential backoff)
5- MAX_RECONNECT_DELAY =
Upper bound for the exponential reconnect backoff, in seconds
60- MAX_RECONNECT_ATTEMPTS =
Maximum reconnection attempts (nil for infinite)
nil
Instance Attribute Summary collapse
-
#app_name ⇒ Object
readonly
Returns the value of attribute app_name.
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#connect {|self| ... } ⇒ self
Connect to the WebSocket endpoint.
-
#connected? ⇒ Boolean
Check if WebSocket is connected.
-
#disconnect ⇒ self
Disconnect from the WebSocket.
-
#initialize(base_url, api_key, app_name, options = {}) ⇒ WebSocket
constructor
Initialize a new WebSocket client.
-
#on(event_type, &block) ⇒ self
Register a callback for a specific event type.
-
#send_message?(message) ⇒ Boolean
Send a message through the WebSocket.
Constructor Details
#initialize(base_url, api_key, app_name, options = {}) ⇒ WebSocket
Initialize a new WebSocket client
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 64 def initialize(base_url, api_key, app_name, = {}) @base_url = base_url @api_key = api_key @app_name = app_name @callbacks = {} @connected = false @should_reconnect = .fetch(:auto_reconnect, true) @reconnect_attempts = 0 @logger = [:logger] || Logger.new($stdout) @ping_interval = .fetch(:ping_interval, PING_INTERVAL) @reconnect_delay = .fetch(:reconnect_delay, RECONNECT_DELAY) initialize_io_state end |
Instance Attribute Details
#app_name ⇒ Object (readonly)
Returns the value of attribute app_name.
40 41 42 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 40 def app_name @app_name end |
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
40 41 42 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 40 def callbacks @callbacks end |
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
40 41 42 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 40 def connected @connected end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
40 41 42 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 40 def url @url end |
Instance Method Details
#connect {|self| ... } ⇒ self
Connect to the WebSocket endpoint
82 83 84 85 86 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 82 def connect(&block) @on_connect_callback = block @connection_thread = Thread.new { connection_loop } self end |
#connected? ⇒ Boolean
Check if WebSocket is connected
120 121 122 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 120 def connected? !!(@connected && @driver && @driver.state == :open) end |
#disconnect ⇒ self
Disconnect from the WebSocket
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 102 def disconnect @should_reconnect = false stop_ping_timer close_connection wake_sleepers thread = @connection_thread @connection_thread = nil thread&.join(2) unless thread == Thread.current @connected = false @logger.info 'WebSocket disconnected' self end |
#on(event_type, &block) ⇒ self
Register a callback for a specific event type
93 94 95 96 97 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 93 def on(event_type, &block) @callbacks[event_type.to_s] = [] unless @callbacks.key?(event_type.to_s) @callbacks[event_type.to_s] << block self end |
#send_message?(message) ⇒ Boolean
Send a message through the WebSocket
128 129 130 131 132 133 134 135 136 137 |
# File 'lib/ruby-asterisk/ari/websocket.rb', line 128 def () driver = @driver return false unless @connected && driver && driver.state == :open data = .is_a?(Hash) ? JSON.generate() : @driver_monitor.synchronize { driver.text(data) } true rescue IOError, SystemCallError false end |