Class: Playwright::Transport
- Inherits:
-
Object
- Object
- Playwright::Transport
- Defined in:
- lib/playwright/transport.rb
Overview
Defined Under Namespace
Classes: AlreadyDisconnectedError
Instance Method Summary collapse
-
#initialize(playwright_cli_executable_path:) ⇒ Transport
constructor
A new instance of Transport.
- #on_message_received(&block) ⇒ Object
-
#run ⇒ Object
Start ‘playwright-cli run-driver`.
- #send_message(message) ⇒ Object
-
#stop ⇒ Object
Terminate playwright-cli driver.
Constructor Details
#initialize(playwright_cli_executable_path:) ⇒ Transport
Returns a new instance of Transport.
12 13 14 15 |
# File 'lib/playwright/transport.rb', line 12 def initialize(playwright_cli_executable_path:) @driver_executable_path = playwright_cli_executable_path @debug = ENV['DEBUG'].to_s == 'true' || ENV['DEBUG'].to_s == '1' end |
Instance Method Details
#on_message_received(&block) ⇒ Object
17 18 19 |
# File 'lib/playwright/transport.rb', line 17 def (&block) @on_message = block end |
#run ⇒ Object
Note:
This method blocks until playwright-cli exited. Consider using Thread or Future.
Start ‘playwright-cli run-driver`
41 42 43 44 45 46 47 48 |
# File 'lib/playwright/transport.rb', line 41 def run @stdin, @stdout, @stderr, @thread = Open3.popen3(@driver_executable_path, 'run-driver') Thread.new { handle_stdout } Thread.new { handle_stderr } @thread.join end |
#send_message(message) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/playwright/transport.rb', line 24 def () () if @debug msg = JSON.dump() @stdin.write([msg.size].pack('V')) # unsigned 32bit, little endian @stdin.write(msg) rescue Errno::EPIPE raise AlreadyDisconnectedError.new('send_message failed') end |
#stop ⇒ Object
Terminate playwright-cli driver.
34 35 36 |
# File 'lib/playwright/transport.rb', line 34 def stop [@stdin, @stdout, @stderr].each { |io| io.close unless io.closed? } end |