Class: Puppeteer::Connection
Defined Under Namespace
Classes: MessageCallback, ProtocolError, RequestDebugPrinter, ResponseDebugPrinter
Class Method Summary
collapse
Instance Method Summary
collapse
#add_event_listener, #emit_event, #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
|
# File 'lib/puppeteer/connection.rb', line 39
def initialize(url, transport, delay = 0)
@url = url
@last_id = 0
@callbacks = {}
@delay = delay
@transport = transport
@transport.on_message do |data|
async_handle_message(JSON.parse(data))
end
@transport.on_close do |reason, code|
handle_close(reason, code)
end
@sessions = {}
@closed = false
end
|
Class Method Details
.from_session(session) ⇒ Object
57
58
59
|
# File 'lib/puppeteer/connection.rb', line 57
def self.from_session(session)
session.connection
end
|
Instance Method Details
#async_send_message(method, params = {}) ⇒ Object
77
78
79
80
81
82
|
# File 'lib/puppeteer/connection.rb', line 77
def async_send_message(method, params = {})
id = raw_send(message: { method: method, params: params })
promise = resolvable_future
@callbacks[id] = MessageCallback.new(method: method, promise: promise)
promise
end
|
#create_session(target_info) ⇒ CDPSession
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
# File 'lib/puppeteer/connection.rb', line 239
def create_session(target_info)
result = send_message('Target.attachToTarget', targetId: target_info.target_id, flatten: true)
session_id = result['sessionId']
20.times do
if @sessions[session_id]
return @sessions[session_id]
end
sleep 0.1
end
end
|
#dispose ⇒ Object
232
233
234
235
|
# File 'lib/puppeteer/connection.rb', line 232
def dispose
handle_close
@transport.close
end
|
#on_close(&block) ⇒ Object
224
225
226
|
# File 'lib/puppeteer/connection.rb', line 224
def on_close(&block)
@on_close = block
end
|
#on_message(&block) ⇒ Object
228
229
230
|
# File 'lib/puppeteer/connection.rb', line 228
def on_message(&block)
@on_message = block
end
|
#raw_send(message:) ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/puppeteer/connection.rb', line 88
def raw_send(message:)
id = generate_id
payload = JSON.fast_generate(message.compact.merge(id: id))
@transport.send_text(payload)
request_debug_printer.handle_payload(payload)
id
end
|
#send_message(method, params = {}) ⇒ Object
73
74
75
|
# File 'lib/puppeteer/connection.rb', line 73
def send_message(method, params = {})
await async_send_message(method, params)
end
|
#session(session_id) ⇒ ?CDPSession
63
64
65
|
# File 'lib/puppeteer/connection.rb', line 63
def session(session_id)
@sessions[session_id]
end
|
#url ⇒ Object
67
68
69
|
# File 'lib/puppeteer/connection.rb', line 67
def url
@url
end
|