Class: Lightpanda::Client::WebSocket

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options) ⇒ WebSocket

Returns a new instance of WebSocket.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/lightpanda/client/web_socket.rb', line 12

def initialize(url, options)
  @url = url
  @options = options
  @socket = nil
  @driver = nil
  @thread = nil
  @status = :closed
  @messages = Queue.new

  connect
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



10
11
12
# File 'lib/lightpanda/client/web_socket.rb', line 10

def messages
  @messages
end

#urlObject (readonly)

Returns the value of attribute url.



10
11
12
# File 'lib/lightpanda/client/web_socket.rb', line 10

def url
  @url
end

Instance Method Details

#closeObject



30
31
32
33
34
35
36
37
38
# File 'lib/lightpanda/client/web_socket.rb', line 30

def close
  return if @status == :closed

  @status = :closing
  @driver&.close
  @thread&.kill
  @socket&.close
  @status = :closed
end

#closed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/lightpanda/client/web_socket.rb', line 40

def closed?
  @status == :closed
end

#open?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/lightpanda/client/web_socket.rb', line 44

def open?
  @status == :open
end

#send_message(message) ⇒ Object

Raises:



24
25
26
27
28
# File 'lib/lightpanda/client/web_socket.rb', line 24

def send_message(message)
  raise DeadBrowserError, "WebSocket is not open" unless @status == :open

  @driver.text(message)
end

#write(data) ⇒ Object



48
49
50
51
52
# File 'lib/lightpanda/client/web_socket.rb', line 48

def write(data)
  @socket.write(data)
rescue Errno::EPIPE, Errno::ECONNRESET, IOError
  @status = :closed
end