Class: Playwright::Transport
- Inherits:
-
Object
- Object
- Playwright::Transport
- Defined in:
- lib/playwright/transport.rb
Overview
Defined Under Namespace
Classes: AlreadyDisconnectedError
Instance Method Summary collapse
-
#async_run ⇒ Object
Start ‘playwright-cli run-driver`.
-
#initialize(playwright_cli_executable_path:) ⇒ Transport
constructor
A new instance of Transport.
- #on_driver_closed(&block) ⇒ Object
- #on_driver_crashed(&block) ⇒ Object
- #on_message_received(&block) ⇒ Object
- #send_message(message) ⇒ Object
-
#stop ⇒ Object
Terminate playwright-cli driver.
Constructor Details
#initialize(playwright_cli_executable_path:) ⇒ Transport
Returns a new instance of Transport.
11 12 13 14 15 |
# File 'lib/playwright/transport.rb', line 11 def initialize(playwright_cli_executable_path:) @driver_executable_path = playwright_cli_executable_path @debug = ENV['DEBUG'].to_s == 'true' || ENV['DEBUG'].to_s == '1' @mutex = Mutex.new 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`
52 53 54 55 56 57 58 |
# File 'lib/playwright/transport.rb', line 52 def async_run @stdin, @stdout, @stderr, @thread = run_driver_with_open3 @stdin.binmode # Ensure Strings are written 1:1 without encoding conversion, necessary for integer values Thread.new { handle_stdout } Thread.new { handle_stderr } end |
#on_driver_closed(&block) ⇒ Object
21 22 23 |
# File 'lib/playwright/transport.rb', line 21 def on_driver_closed(&block) @on_driver_closed = block end |
#on_driver_crashed(&block) ⇒ Object
25 26 27 |
# File 'lib/playwright/transport.rb', line 25 def on_driver_crashed(&block) @on_driver_crashed = block end |
#on_message_received(&block) ⇒ Object
17 18 19 |
# File 'lib/playwright/transport.rb', line 17 def (&block) @on_message = block end |
#send_message(message) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/playwright/transport.rb', line 32 def () () if @debug msg = JSON.dump() @mutex.synchronize { @stdin.write([msg.bytes.length].pack('V')) # unsigned 32bit, little endian, real byte size instead of chars @stdin.write(msg) # write UTF-8 in binary mode as byte stream } rescue Errno::EPIPE, IOError raise AlreadyDisconnectedError.new('send_message failed') end |
#stop ⇒ Object
Terminate playwright-cli driver.
44 45 46 47 |
# File 'lib/playwright/transport.rb', line 44 def stop [@stdin, @stdout, @stderr].each { |io| io.close unless io.closed? } @thread&.terminate end |