Class: Playwright::WebSocketTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/playwright/web_socket_transport.rb

Overview

Defined Under Namespace

Classes: AlreadyDisconnectedError

Instance Method Summary collapse

Constructor Details

#initialize(ws_endpoint:) ⇒ WebSocketTransport

Returns a new instance of WebSocketTransport.

Parameters:

  • ws_endpoint (String)

    EndpointURL of WebSocket



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_runObject

Note:

This method blocks until playwright-cli exited. Consider using Thread or Future.

Start ‘playwright-cli run-driver`



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/playwright/web_socket_transport.rb', line 62

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 |error_message|
    promise.reject(WebSocketClient::TransportError.new(error_message))
  end

  # Some messages can be sent just after start, before setting @ws.on_message
  # So set this handler before ws.start.
  ws.on_message do |data|
    handle_on_message(data)
  end

  ws.start
  @ws = promise.value!
  @ws.on_close do |reason, code|
    puts "[WebSocketTransport] closed with code: #{code}, reason: #{reason}"
    @on_driver_closed&.call(reason, code)
  end
  @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_closed(&block) ⇒ Object



18
19
20
# File 'lib/playwright/web_socket_transport.rb', line 18

def on_driver_closed(&block)
  @on_driver_closed = block
end

#on_driver_crashed(&block) ⇒ Object



22
23
24
# File 'lib/playwright/web_socket_transport.rb', line 22

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 on_message_received(&block)
  @on_message = block
end

#send_message(message) ⇒ Object

Parameters:

  • message (Hash)


29
30
31
32
33
34
35
36
# File 'lib/playwright/web_socket_transport.rb', line 29

def send_message(message)
  debug_send_message(message) if @debug
  msg = JSON.dump(message)

  @ws.send_text(msg)
rescue Errno::EPIPE, IOError
  raise AlreadyDisconnectedError.new('send_message failed')
end

#stopObject

Terminate playwright-cli driver.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/playwright/web_socket_transport.rb', line 39

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!(2)
end