Class: Puppeteer::CDPSession
Defined Under Namespace
Classes: Error
Instance Attribute 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(connection, target_type, session_id) ⇒ CDPSession
Returns a new instance of CDPSession.
11
12
13
14
15
16
17
|
# File 'lib/puppeteer/cdp_session.rb', line 11
def initialize(connection, target_type, session_id)
@callbacks = {}
@connection = connection
@target_type = target_type
@session_id = session_id
@pending_messages = {}
end
|
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
19
20
21
|
# File 'lib/puppeteer/cdp_session.rb', line 19
def connection
@connection
end
|
Instance Method Details
#async_send_message(method, params = {}) ⇒ Object
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/puppeteer/cdp_session.rb', line 31
def async_send_message(method, params = {})
if !@connection
raise Error.new("Protocol error (#{method}): Session closed. Most likely the #{@target_type} has been closed.")
end
id = @connection.raw_send(message: { sessionId: @session_id, method: method, params: params })
promise = resolvable_future
callback = Puppeteer::Connection::MessageCallback.new(method: method, promise: promise)
if pending_message = @pending_messages.delete(id)
debug_puts "Pending message (id: #{id}) is handled"
callback_with_message(callback, pending_message)
else
@callbacks[id] = callback
end
promise
end
|
#detach ⇒ Object
82
83
84
85
86
87
|
# File 'lib/puppeteer/cdp_session.rb', line 82
def detach
if !@connection
raise Error.new("Session already detarched. Most likely the #{@target_type} has been closed.")
end
@connection.send_message('Target.detachFromTarget', sessionId: @session_id)
end
|
#handle_closed ⇒ Object
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/puppeteer/cdp_session.rb', line 89
def handle_closed
@callbacks.each_value do |callback|
callback.reject(
Puppeteer::Connection::ProtocolError.new(
method: callback.method,
error_message: 'Target Closed.'))
end
@callbacks.clear
@connection = nil
emit_event 'Events.CDPSession.Disconnected'
end
|
#handle_message(message) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/puppeteer/cdp_session.rb', line 48
def handle_message(message)
if message['id']
if callback = @callbacks.delete(message['id'])
callback_with_message(callback, message)
else
debug_puts "unknown id: #{id}. Store it into pending message"
message_id = message['id']
@pending_messages[message_id] = message
Concurrent::Promises.schedule(0.16, message_id) do |id|
if @pending_messages.delete(id)
raise Error.new("unknown id: #{id}")
end
end
end
else
emit_event message['method'], message['params']
end
end
|
#send_message(method, params = {}) ⇒ Object
24
25
26
|
# File 'lib/puppeteer/cdp_session.rb', line 24
def send_message(method, params = {})
await async_send_message(method, params)
end
|