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.



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

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



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

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


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

def on_close(&block)
  @on_close = block
end

#on_error(&block) ⇒ Object

Parameters:

  • block (Proc(error_message: String))


101
102
103
# File 'lib/puppeteer/web_socket.rb', line 101

def on_error(&block)
  @on_error = block
end

#on_message(&block) ⇒ Object



105
106
107
# File 'lib/puppeteer/web_socket.rb', line 105

def on_message(&block)
  @on_message = block
end

#on_open(&block) ⇒ Object



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

def on_open(&block)
  @on_open = block
end

#send_text(message) ⇒ Object

Parameters:

  • message (String)


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

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