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, #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?

Returns:

  • (Boolean)


60
61
62
# File 'lib/puppeteer/connection.rb', line 60

def closed?
  @closed
end

#create_session(target_info) ⇒ CDPSession

Parameters:

  • targetInfo (Protocol.Target.TargetInfo)

Returns:



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

#disposeObject



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|

# play with 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:)
  # In original puppeteer (JS) implementation,
  # id is generated here using #generate_id and the id argument is not passed to #raw_send.
  #
  # However with concurrent-ruby, '#handle_message' is sometimes called
  # just soon after @transport.send_text and **before returning the id.**
  #
  # So we have to know the message id in advance before send_text.
  #
  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

Parameters:

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


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

Parameters:

  • sessionId (string)

Returns:



80
81
82
# File 'lib/puppeteer/connection.rb', line 80

def session(session_id)
  @sessions[session_id]
end

#urlObject



84
85
86
# File 'lib/puppeteer/connection.rb', line 84

def url
  @url
end