Class: SlackSocketModeBot::Transport::WebSocket
- Inherits:
-
Object
- Object
- SlackSocketModeBot::Transport::WebSocket
- Defined in:
- lib/transport/websocket.rb
Overview
This class heavily leverages async and async-websocket to keep a connection alive to the URL provided by the client It will perform as expected for a Slack Socket Mode client: acknowledge all events and refresh when needed Moreover it will use the client’s callback for processing events
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#connect! ⇒ Object
rubocop:disable Metrics.
-
#initialize(client:, logger: nil) ⇒ WebSocket
constructor
A new instance of WebSocket.
Constructor Details
#initialize(client:, logger: nil) ⇒ WebSocket
Returns a new instance of WebSocket.
12 13 14 15 16 17 18 |
# File 'lib/transport/websocket.rb', line 12 def initialize(client:, logger: nil) @client = client @logger = logger || Logger.new($stdout) @restart = Async::Notification.new @ping_id = 1 end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
10 11 12 |
# File 'lib/transport/websocket.rb', line 10 def client @client end |
Instance Method Details
#connect! ⇒ Object
rubocop:disable Metrics
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 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/transport/websocket.rb', line 21 def connect! Async do |task| trap_sigterm(task) loop do endpoint = Async::HTTP::Endpoint.parse(client.new_socket_url) Async::WebSocket::Client.connect(endpoint) do |connection| @ping_task = task.async do |subtask| subtask.annotate "socket keep-alive" loop do subtask.sleep 50 ping!(connection) if @restart end end @socket_task&.stop @socket_task = task.async do |subtask| subtask.annotate "socket message loop" (connection) rescue StandardError => e @logger.info("Message read failed: #{e.}. Restarting the socket") restart! end # Wait here letting it ping & process messages until we need to reconnect @restart.wait end end end rescue Interrupt puts "Interrupt detected. Exiting..." ensure @ping_task&.stop @socket_task&.stop end |