Class: Playwright::WebSocketTransport
- Inherits:
-
Object
- Object
- Playwright::WebSocketTransport
- Defined in:
- lib/playwright/web_socket_transport.rb
Overview
Defined Under Namespace
Classes: AlreadyDisconnectedError
Instance Method Summary collapse
-
#async_run ⇒ Object
Start `playwright-cli run-driver`.
-
#initialize(ws_endpoint:) ⇒ WebSocketTransport
constructor
A new instance of WebSocketTransport.
- #on_driver_crashed(&block) ⇒ Object
- #on_message_received(&block) ⇒ Object
- #send_message(message) ⇒ Object
-
#stop ⇒ Object
Terminate playwright-cli driver.
Constructor Details
#initialize(ws_endpoint:) ⇒ WebSocketTransport
Returns a new instance of WebSocketTransport.
9 10 11 12 |
# File 'lib/playwright/web_socket_transport.rb', line 9 def initialize(ws_endpoint:) @ws_endpoint = ws_endpoint @debug = ENV['DEBUG'].to_s == 'true' || ENV['DEBUG'].to_s == '1' end |
Instance Method Details
#async_run ⇒ Object
Note:
This method blocks until playwright-cli exited. Consider using Thread or Future.
Start `playwright-cli run-driver`
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 |
# File 'lib/playwright/web_socket_transport.rb', line 58 def async_run ws = WebSocketClient.new( url: @ws_endpoint, max_payload_size: 256 * 1024 * 1024, # 256MB ) promise = Concurrent::Promises.resolvable_future ws.on_open do promise.fulfill(ws) end ws.on_error do || promise.reject(WebSocketClient::TransportError.new()) end # Some messages can be sent just after start, before setting @ws.on_message # So set this handler before ws.start. ws. do |data| (data) end ws.start @ws = promise.value! @ws.on_error do |error| puts "[WebSocketTransport] error: #{error}" @on_driver_crashed&.call end rescue Errno::ECONNREFUSED => err raise WebSocketClient::TransportError.new(err) end |
#on_driver_crashed(&block) ⇒ Object
18 19 20 |
# File 'lib/playwright/web_socket_transport.rb', line 18 def on_driver_crashed(&block) @on_driver_crashed = block end |
#on_message_received(&block) ⇒ Object
14 15 16 |
# File 'lib/playwright/web_socket_transport.rb', line 14 def (&block) @on_message = block end |
#send_message(message) ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/playwright/web_socket_transport.rb', line 25 def () () if @debug msg = JSON.dump() @ws.send_text(msg) rescue Errno::EPIPE, IOError raise AlreadyDisconnectedError.new('send_message failed') end |
#stop ⇒ Object
Terminate playwright-cli driver.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/playwright/web_socket_transport.rb', line 35 def stop return unless @ws future = Concurrent::Promises.resolvable_future @ws.on_close do future.fulfill(nil) end begin @ws.close rescue EOFError => err # ignore EOLError. The connection is already closed. future.fulfill(err) end # Wait for closed actually. future.value! end |