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_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`
48 49 50 51 52 53 54 |
# File 'lib/playwright/transport.rb', line 48 def async_run @stdin, @stdout, @stderr, @thread = Open3.popen3("#{@driver_executable_path} run-driver") @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_crashed(&block) ⇒ Object
21 22 23 |
# File 'lib/playwright/transport.rb', line 21 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
28 29 30 31 32 33 34 35 36 37 |
# File 'lib/playwright/transport.rb', line 28 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.
40 41 42 43 |
# File 'lib/playwright/transport.rb', line 40 def stop [@stdin, @stdout, @stderr].each { |io| io.close unless io.closed? } @thread&.terminate end |