Class: Puppeteer::WebSocketTransport

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/web_socket_transport.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(web_socket) ⇒ WebSocketTransport

Returns a new instance of WebSocketTransport.

Parameters:

  • web_socket (!WebSocket::Driver)


20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/puppeteer/web_socket_transport.rb', line 20

def initialize(web_socket)
  @ws = web_socket
  @ws.on_message do |data|
    @on_message&.call(data)
  end
  @ws.on_close do |reason, code|
    @on_close&.call(reason, code)
  end
  @ws.on_error do |error|
    # Silently ignore all errors - we don't know what to do with them.
  end
end

Class Method Details

.create(url) ⇒ Puppeteer::WebSocketTransport

Parameters:

  • url (string)

Returns:



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/puppeteer/web_socket_transport.rb', line 4

def self.create(url)
  ws = Puppeteer::WebSocket.new(
    url: url,
    max_payload_size: 256 * 1024 * 1024, # 256MB
  )
  (resolvable_future do |future|
    ws.on_open do
      future.fulfill(Puppeteer::WebSocketTransport.new(ws))
    end
    ws.on_error do |error_message|
      future.reject(Puppeteer::WebSocket::TransportError.new(error_message))
    end
  end).value!
end

Instance Method Details

#closeObject



38
39
40
41
42
# File 'lib/puppeteer/web_socket_transport.rb', line 38

def close
  @ws.close
rescue EOFError
  # ignore EOLError. The connection is already closed.
end

#on_close(&block) ⇒ Object



44
45
46
# File 'lib/puppeteer/web_socket_transport.rb', line 44

def on_close(&block)
  @on_close = block
end

#on_message(&block) ⇒ Object



48
49
50
# File 'lib/puppeteer/web_socket_transport.rb', line 48

def on_message(&block)
  @on_message = block
end

#send_text(message) ⇒ Object

Parameters:

  • message (String)


34
35
36
# File 'lib/puppeteer/web_socket_transport.rb', line 34

def send_text(message)
  @ws.send_text(message)
end