Class: Puppeteer::Connection
Defined Under Namespace
Classes: MessageCallback, ProtocolError, RequestDebugPrinter, ResponseDebugPrinter
Class Method Summary
collapse
Instance Method Summary
collapse
#add_event_listener, #emit_event, #observe_first, #on_event, #remove_event_listener
Methods included from DebugPrint
#debug_print, #debug_puts
Constructor Details
#initialize(url, transport, delay = 0) ⇒ Connection
Returns a new instance of Connection.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/puppeteer/connection.rb', line 39
def initialize(url, transport, delay = 0)
@url = url
@last_id = 0
@callbacks = Concurrent::Hash.new
@delay = delay
@transport = transport
@transport.on_message do |data|
message = JSON.parse(data)
sleep_before_handling_message(message)
async_handle_message(message)
end
@transport.on_close do |reason, code|
handle_close
end
@sessions = Concurrent::Hash.new
@closed = false
end
|
Class Method Details
.from_session(session) ⇒ Object
74
75
76
|
# File 'lib/puppeteer/connection.rb', line 74
def self.from_session(session)
session.connection
end
|
Instance Method Details
#async_send_message(method, params = {}) ⇒ Object
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/puppeteer/connection.rb', line 94
def async_send_message(method, params = {})
promise = resolvable_future
generate_id do |id|
@callbacks[id] = MessageCallback.new(method: method, promise: promise)
raw_send(id: id, message: { method: method, params: params })
end
promise
end
|
#closed? ⇒ Boolean
used only in Browser#connected?
60
61
62
|
# File 'lib/puppeteer/connection.rb', line 60
def closed?
@closed
end
|
#create_session(target_info) ⇒ CDPSession
273
274
275
276
277
|
# File 'lib/puppeteer/connection.rb', line 273
def create_session(target_info)
result = send_message('Target.attachToTarget', targetId: target_info.target_id, flatten: true)
session_id = result['sessionId']
@sessions[session_id]
end
|
#dispose ⇒ Object
266
267
268
269
|
# File 'lib/puppeteer/connection.rb', line 266
def dispose
handle_close
@transport.close
end
|
#generate_id(&block) ⇒ Object
package private. not intended to use externally.
“`usage connection.generate_id do |generated_id|
end ““
113
114
115
|
# File 'lib/puppeteer/connection.rb', line 113
def generate_id(&block)
block.call(@last_id += 1)
end
|
#on_close(&block) ⇒ Object
258
259
260
|
# File 'lib/puppeteer/connection.rb', line 258
def on_close(&block)
@on_close = block
end
|
#on_message(&block) ⇒ Object
262
263
264
|
# File 'lib/puppeteer/connection.rb', line 262
def on_message(&block)
@on_message = block
end
|
#raw_send(id:, message:) ⇒ Object
package private. not intended to use externally.
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/puppeteer/connection.rb', line 118
def raw_send(id:, message:)
payload = JSON.fast_generate(message.compact.merge(id: id))
@transport.send_text(payload)
request_debug_printer.handle_payload(payload)
end
|
#send_message(method, params = {}) ⇒ Object
90
91
92
|
# File 'lib/puppeteer/connection.rb', line 90
def send_message(method, params = {})
await async_send_message(method, params)
end
|
#session(session_id) ⇒ ?CDPSession
80
81
82
|
# File 'lib/puppeteer/connection.rb', line 80
def session(session_id)
@sessions[session_id]
end
|
#url ⇒ Object
84
85
86
|
# File 'lib/puppeteer/connection.rb', line 84
def url
@url
end
|