Class: Puppeteer::WebSocket

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

Overview

Defined Under Namespace

Classes: DriverImpl

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.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/puppeteer/web_socket.rb', line 31

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.
  end
end

Instance Method Details

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



76
77
78
79
80
# File 'lib/puppeteer/web_socket.rb', line 76

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))


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

def on_close(&block)
  @on_close = block
end

#on_error(&block) ⇒ Object

Parameters:

  • block (Proc(error_message: String))


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

def on_error(&block)
  @on_error = block
end

#on_message(&block) ⇒ Object



96
97
98
# File 'lib/puppeteer/web_socket.rb', line 96

def on_message(&block)
  @on_message = block
end

#on_open(&block) ⇒ Object



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

def on_open(&block)
  @on_open = block
end

#send_text(message) ⇒ Object

Parameters:

  • message (String)


71
72
73
74
# File 'lib/puppeteer/web_socket.rb', line 71

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