Class: ToggleCraft::SSEConnection
- Inherits:
-
Object
- Object
- ToggleCraft::SSEConnection
- Defined in:
- lib/togglecraft/sse_connection.rb
Overview
SSE (Server-Sent Events) connection manager Manages persistent connection to ToggleCraft SSE server with heartbeat
Instance Attribute Summary collapse
-
#connected ⇒ Object
readonly
Returns the value of attribute connected.
-
#connection_id ⇒ Object
readonly
Returns the value of attribute connection_id.
-
#sdk_key ⇒ Object
readonly
Returns the value of attribute sdk_key.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#connect ⇒ void
Connect to SSE server.
-
#connected? ⇒ Boolean
Check if connected to SSE server.
-
#disconnect ⇒ void
Disconnect from SSE server.
-
#initialize(url:, sdk_key:, **options) ⇒ SSEConnection
constructor
Initialize SSE connection.
-
#reconnect ⇒ void
Reconnect to SSE server.
Constructor Details
#initialize(url:, sdk_key:, **options) ⇒ SSEConnection
Initialize SSE connection
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/togglecraft/sse_connection.rb', line 17 def initialize(url:, sdk_key:, **) @url = url.sub(%r{/cable$}, '') # Remove /cable if present @sdk_key = sdk_key = { reconnect_interval: 1, max_reconnect_interval: 30, max_reconnect_attempts: 10, slow_reconnect_interval: 60, heartbeat_domain: 'https://togglecraft.io', heartbeat_interval: 300, # 5 minutes debug: false }.merge() @connected = false @connection_id = nil @reconnect_attempts = 0 @should_reconnect = true # Callbacks = [:on_message] || proc { } @on_connect = [:on_connect] || proc { } @on_disconnect = [:on_disconnect] || proc { } @on_error = [:on_error] || proc { } # Threads @connection_thread = nil @heartbeat_thread = nil @heartbeat_timer = nil end |
Instance Attribute Details
#connected ⇒ Object (readonly)
Returns the value of attribute connected.
11 12 13 |
# File 'lib/togglecraft/sse_connection.rb', line 11 def connected @connected end |
#connection_id ⇒ Object (readonly)
Returns the value of attribute connection_id.
11 12 13 |
# File 'lib/togglecraft/sse_connection.rb', line 11 def connection_id @connection_id end |
#sdk_key ⇒ Object (readonly)
Returns the value of attribute sdk_key.
11 12 13 |
# File 'lib/togglecraft/sse_connection.rb', line 11 def sdk_key @sdk_key end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
11 12 13 |
# File 'lib/togglecraft/sse_connection.rb', line 11 def url @url end |
Instance Method Details
#connect ⇒ void
This method returns an undefined value.
Connect to SSE server
49 50 51 52 53 54 |
# File 'lib/togglecraft/sse_connection.rb', line 49 def connect return if @connected @should_reconnect = true start_connection end |
#connected? ⇒ Boolean
Check if connected to SSE server
78 79 80 |
# File 'lib/togglecraft/sse_connection.rb', line 78 def connected? @connected end |
#disconnect ⇒ void
This method returns an undefined value.
Disconnect from SSE server
58 59 60 61 62 63 64 65 |
# File 'lib/togglecraft/sse_connection.rb', line 58 def disconnect log 'Disconnecting' @should_reconnect = false @connection_id = nil stop_heartbeat stop_connection @connected = false end |
#reconnect ⇒ void
This method returns an undefined value.
Reconnect to SSE server
69 70 71 72 73 74 |
# File 'lib/togglecraft/sse_connection.rb', line 69 def reconnect disconnect @should_reconnect = true @reconnect_attempts = 0 connect end |