Class: Capybara::Cuprite::Browser::WebSocket
- Inherits:
-
Object
- Object
- Capybara::Cuprite::Browser::WebSocket
- Extended by:
- Forwardable
- Defined in:
- lib/capybara/cuprite/browser/web_socket.rb
Instance Attribute Summary collapse
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#initialize(url, logger) ⇒ WebSocket
constructor
A new instance of WebSocket.
- #on_message(event) ⇒ Object
- #send_message(data) ⇒ Object
- #write(data) ⇒ Object
Constructor Details
#initialize(url, logger) ⇒ WebSocket
Returns a new instance of WebSocket.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/capybara/cuprite/browser/web_socket.rb', line 17 def initialize(url, logger) @url = url @logger = logger uri = URI.parse(@url) @sock = TCPSocket.new(uri.host, uri.port) @driver = ::WebSocket::Driver.client(self) @messages = Queue.new @driver.on(:message, &method(:on_message)) @thread = Thread.new do begin while data = @sock.readpartial(512) @driver.parse(data) end rescue EOFError, Errno::ECONNRESET @messages.close end end @thread.priority = 1 @driver.start end |
Instance Attribute Details
#messages ⇒ Object (readonly)
Returns the value of attribute messages.
15 16 17 |
# File 'lib/capybara/cuprite/browser/web_socket.rb', line 15 def @messages end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
15 16 17 |
# File 'lib/capybara/cuprite/browser/web_socket.rb', line 15 def url @url end |
Instance Method Details
#on_message(event) ⇒ Object
49 50 51 52 53 |
# File 'lib/capybara/cuprite/browser/web_socket.rb', line 49 def (event) data = JSON.parse(event.data) @messages.push(data) @logger&.puts("[#{Time.now.to_f - @started_at}] <<< #{event.data}\n") end |
#send_message(data) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/capybara/cuprite/browser/web_socket.rb', line 42 def (data) @started_at ||= Time.now.to_f json = data.to_json @driver.text(json) @logger&.puts("\n\n[#{Time.now.to_f - @started_at}] >>> #{json}") end |
#write(data) ⇒ Object
55 56 57 |
# File 'lib/capybara/cuprite/browser/web_socket.rb', line 55 def write(data) @sock.write(data) end |