Class: Capybara::Lightpanda::Client::WebSocket

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/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.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 15

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

  connect
end

Instance Attribute Details

#messagesObject (readonly)

Returns the value of attribute messages.



13
14
15
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 13

def messages
  @messages
end

#urlObject (readonly)

Returns the value of attribute url.



13
14
15
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 13

def url
  @url
end

Instance Method Details

#closeObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 36

def close
  return if @status == :closed

  @status = :closing
  @messages.close
  @driver_mutex.synchronize { @driver&.close }
  @thread&.join(1) || @thread&.kill
  @socket&.close
  @status = :closed
end

#closed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 47

def closed?
  @status == :closed || @status == :error
end

#send_message(message) ⇒ Object

Raises:



29
30
31
32
33
34
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 29

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

  @logger&.puts("\n\n▶ #{@logger.elapsed_time} #{message}")
  @driver_mutex.synchronize { @driver.text(message) }
end

#write(data) ⇒ Object



51
52
53
54
55
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 51

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