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.



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

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.



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

def messages
  @messages
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#closeObject



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

def close
  return if @status == :closed

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @status == :closed
end

#open?Boolean

Returns:

  • (Boolean)


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

def open?
  @status == :open
end

#send_message(message) ⇒ Object

Raises:



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

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



53
54
55
56
57
58
# File 'lib/capybara/lightpanda/client/web_socket.rb', line 53

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