Class: Puppeteer::WebSocket

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

Overview

Defined Under Namespace

Classes: DriverImpl, TransportError

Constant Summary collapse

STATE_CONNECTING =
0
STATE_OPENED =
1
STATE_CLOSING =
2
STATE_CLOSED =
3

Instance Method Summary collapse

Constructor Details

#initialize(url:, max_payload_size:) ⇒ WebSocket

Returns a new instance of WebSocket.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/puppeteer/web_socket.rb', line 37

def initialize(url:, max_payload_size:)
  @impl = DriverImpl.new(url)
  @driver = ::WebSocket::Driver.client(@impl, max_length: max_payload_size)

  setup
  @driver.start

  Thread.new do
    wait_for_data until @ready_state >= STATE_CLOSING
  rescue EOFError
    # Google Chrome was gone.
    # We have nothing todo. Just finish polling.
    if @ready_state < STATE_CLOSING
      handle_on_close(reason: 'Going Away', code: 1001)
    end
  end
end

Instance Method Details

#close(code: 1000, reason: "") ⇒ Object



87
88
89
90
91
# File 'lib/puppeteer/web_socket.rb', line 87

def close(code: 1000, reason: "")
  return if @ready_state >= STATE_CLOSING
  @ready_state = STATE_CLOSING
  @driver.close(reason, code)
end

#on_close(&block) ⇒ Object

Parameters:

  • block (Proc(reason: String, code: Numeric))


98
99
100
# File 'lib/puppeteer/web_socket.rb', line 98

def on_close(&block)
  @on_close = block
end

#on_error(&block) ⇒ Object

Parameters:

  • block (Proc(error_message: String))


103
104
105
# File 'lib/puppeteer/web_socket.rb', line 103

def on_error(&block)
  @on_error = block
end

#on_message(&block) ⇒ Object



107
108
109
# File 'lib/puppeteer/web_socket.rb', line 107

def on_message(&block)
  @on_message = block
end

#on_open(&block) ⇒ Object



93
94
95
# File 'lib/puppeteer/web_socket.rb', line 93

def on_open(&block)
  @on_open = block
end

#send_text(message) ⇒ Object

Parameters:

  • message (String)


82
83
84
85
# File 'lib/puppeteer/web_socket.rb', line 82

def send_text(message)
  return if @ready_state >= STATE_CLOSING
  @driver.text(message)
end