Class: Puppeteer::Bidi::Transport
- Inherits:
-
Object
- Object
- Puppeteer::Bidi::Transport
- Defined in:
- lib/puppeteer/bidi/transport.rb,
sig/puppeteer/bidi/transport.rbs
Overview
Transport handles WebSocket communication with BiDi server This is the lowest layer that manages raw WebSocket send/receive
Defined Under Namespace
Classes: ClosedError
Instance Attribute Summary collapse
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#async_send_message(message) ⇒ Object
Send a message to BiDi server.
-
#close ⇒ Object
Close the WebSocket connection.
- #closed? ⇒ Boolean
-
#connect ⇒ Object
Connect to WebSocket and start receiving messages.
- #connected? ⇒ Boolean
- #debug_print_receive(message) ⇒ Object
- #debug_print_send(message) ⇒ Object
-
#initialize(url) ⇒ Transport
constructor
A new instance of Transport.
-
#on_close(&block) ⇒ void
Register close handler.
-
#on_message(&block) ⇒ void
Register message handler.
- #receive_loop(connection) ⇒ Object
Constructor Details
#initialize(url) ⇒ Transport
Returns a new instance of Transport.
18 19 20 21 22 23 24 25 26 27 |
# File 'lib/puppeteer/bidi/transport.rb', line 18 def initialize(url) @url = url @endpoint = nil @connection = nil @task = nil @connected = false @closed = false = nil @on_close = nil end |
Instance Attribute Details
#url ⇒ Object (readonly)
Returns the value of attribute url.
16 17 18 |
# File 'lib/puppeteer/bidi/transport.rb', line 16 def url @url end |
Instance Method Details
#async_send_message(message) ⇒ Object
Send a message to BiDi server
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/puppeteer/bidi/transport.rb', line 56 def () raise ClosedError, 'Transport is closed' if @closed debug_print_send() json = JSON.generate() Async do @connection&.write(json) @connection&.flush end end |
#close ⇒ Object
Close the WebSocket connection
78 79 80 81 82 83 84 85 |
# File 'lib/puppeteer/bidi/transport.rb', line 78 def close return if @closed @closed = true @connection&.close @on_close&.call @task&.stop end |
#closed? ⇒ Boolean
87 88 89 |
# File 'lib/puppeteer/bidi/transport.rb', line 87 def closed? @closed end |
#connect ⇒ Object
Connect to WebSocket and start receiving messages
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/puppeteer/bidi/transport.rb', line 30 def connect connection_promise = Async::Promise.new @task = Async do |task| endpoint = Async::HTTP::Endpoint.parse(@url) # Connect to WebSocket - this matches minibidi's implementation Async::WebSocket::Client.connect(endpoint) do |connection| @connection = connection @connected = true connection_promise.resolve(connection) # Start message receiving loop (this will block until connection closes) receive_loop(connection) end rescue => e warn "Transport connect error: #{e.class} - #{e.message}" warn e.backtrace.join("\n") connection_promise.reject(e) close ensure @connected = false end connection_promise end |
#connected? ⇒ Boolean
91 92 93 |
# File 'lib/puppeteer/bidi/transport.rb', line 91 def connected? @connected && !@closed end |
#debug_print_receive(message) ⇒ Object
124 125 126 127 128 |
# File 'lib/puppeteer/bidi/transport.rb', line 124 def debug_print_receive() if %w[1 true].include?(ENV['DEBUG_PROTOCOL']) puts "RECV << #{JSON.generate(message)}" end end |
#debug_print_send(message) ⇒ Object
118 119 120 121 122 |
# File 'lib/puppeteer/bidi/transport.rb', line 118 def debug_print_send() if %w[1 true].include?(ENV['DEBUG_PROTOCOL']) puts "SEND >> #{JSON.generate(message)}" end end |
#on_close(&block) ⇒ void
This method returns an undefined value.
Register close handler
73 74 75 |
# File 'lib/puppeteer/bidi/transport.rb', line 73 def on_close(&block) @on_close = block end |
#on_message(&block) ⇒ void
This method returns an undefined value.
Register message handler
68 69 70 |
# File 'lib/puppeteer/bidi/transport.rb', line 68 def (&block) = block end |
#receive_loop(connection) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/puppeteer/bidi/transport.rb', line 97 def receive_loop(connection) while ( = connection.read) next if .nil? Async do data = JSON.parse(.to_str) debug_print_receive(data) &.call(data) rescue JSON::ParserError => e warn "Failed to parse BiDi message: #{e.message}" end end rescue IOError, Errno::ECONNRESET, Errno::EPIPE # Connection closed - this is expected during shutdown, no need to warn rescue => e # Only warn for unexpected errors if we weren't intentionally closed warn "Transport receive error: #{e.message}" unless @closed ensure close unless @closed end |