class Selenium::WebDriver::WebSocketConnection
Constants
- RESPONSE_WAIT_INTERVAL
- RESPONSE_WAIT_TIMEOUT
Public Class Methods
new(url:)
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 28 def initialize(url:) @callback_threads = ThreadGroup.new @messages = [] @session_id = nil @url = url process_handshake @socket_thread = attach_socket_listener end
Public Instance Methods
callbacks()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 45 def callbacks @callbacks ||= Hash.new { |callbacks, event| callbacks[event] = [] } end
close()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 39 def close @callback_threads.list.each(&:exit) @socket_thread.exit socket.close end
send_cmd(**payload)
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 49 def send_cmd(**payload) id = next_id data = payload.merge(id: id) data = JSON.generate(data) WebDriver.logger.debug "WebSocket -> #{data}" out_frame = WebSocket::Frame::Outgoing::Client.new(version: ws.version, data: data, type: 'text') socket.write(out_frame.to_s) wait.until { @messages.find { |m| m['id'] == id } } end
Private Instance Methods
attach_socket_listener()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 68 def attach_socket_listener Thread.new do Thread.current.abort_on_exception = true Thread.current.report_on_exception = false until socket.eof? incoming_frame << socket.readpartial(1024) while (frame = incoming_frame.next) message = process_frame(frame) next unless message['method'] params = message['params'] callbacks[message['method']].each do |callback| @callback_threads.add(callback_thread(params, &callback)) end end end end end
callback_thread(params) { |params| ... }
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 106 def callback_thread(params) Thread.new do Thread.current.abort_on_exception = true # We might end up blocked forever when we have an error in event. # For example, if network interception event raises error, # the browser will keep waiting for the request to be proceeded # before returning back to the original thread. In this case, # we should at least print the error. Thread.current.report_on_exception = true yield params end end
incoming_frame()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 89 def incoming_frame @incoming_frame ||= WebSocket::Frame::Incoming::Client.new(version: ws.version) end
next_id()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 142 def next_id @id ||= 0 @id += 1 end
process_frame(frame)
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 93 def process_frame(frame) message = frame.to_s # Firefox will periodically fail on unparsable empty frame return {} if message.empty? message = JSON.parse(message) @messages << message WebDriver.logger.debug "WebSocket <- #{message}" message end
process_handshake()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 63 def process_handshake socket.print(ws.to_s) ws << socket.readpartial(1024) end
socket()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 125 def socket @socket ||= if URI(@url).scheme == 'wss' socket = TCPSocket.new(ws.host, ws.port) socket = OpenSSL::SSL::SSLSocket.new(socket, OpenSSL::SSL::SSLContext.new) socket.sync_close = true socket.connect socket else TCPSocket.new(ws.host, ws.port) end end
wait()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 121 def wait @wait ||= Wait.new(timeout: RESPONSE_WAIT_TIMEOUT, interval: RESPONSE_WAIT_INTERVAL) end
ws()
click to toggle source
# File lib/selenium/webdriver/common/websocket_connection.rb, line 138 def ws @ws ||= WebSocket::Handshake::Client.new(url: @url) end