Class: Puppeteer::Connection

Inherits:
Object
  • Object
show all
Includes:
DebugPrint, EventCallbackable
Defined in:
lib/puppeteer/connection.rb

Defined Under Namespace

Classes: MessageCallback, ProtocolError, RequestDebugPrinter, ResponseDebugPrinter

Class Method Summary collapse

Instance Method Summary collapse

Methods included from EventCallbackable

#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

Parameters:

  • targetInfo (Protocol.Target.TargetInfo)

Returns:



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']

  # Target.attachedToTarget is often notified after the result of Target.attachToTarget.
  # D, [2020-04-04T23:04:30.736311 #91875] DEBUG -- : RECV << {"id"=>2, "result"=>{"sessionId"=>"DA002F8A95B04710502CB40D8430B95A"}}
  # D, [2020-04-04T23:04:30.736649 #91875] DEBUG -- : RECV << {"method"=>"Target.attachedToTarget", "params"=>{"sessionId"=>"DA002F8A95B04710502CB40D8430B95A", "targetInfo"=>{"targetId"=>"EBAB949A7DE63F12CB94268AD3A9976B", "type"=>"page", "title"=>"about:blank", "url"=>"about:blank", "attached"=>true, "browserContextId"=>"46D23767E9B79DD9E589101121F6DADD"}, "waitingForDebugger"=>false}}
  # So we have to wait for "Target.attachedToTarget" a bit.
  20.times do
    if @sessions[session_id]
      return @sessions[session_id]
    end
    sleep 0.1
  end
end

#disposeObject



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

Parameters:

  • method (string)
  • params (!Object=) (defaults to: {})


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

Parameters:

  • sessionId (string)

Returns:



63
64
65
# File 'lib/puppeteer/connection.rb', line 63

def session(session_id)
  @sessions[session_id]
end

#urlObject



67
68
69
# File 'lib/puppeteer/connection.rb', line 67

def url
  @url
end