Class: Perus::Pinger::ChromeCommand
- Defined in:
- lib/perus/pinger/chrome_command.rb
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Command
Instance Method Summary collapse
Methods inherited from Command
abstract!, abstract?, #cleanup, #darwin?, description, human_name, inherited, #initialize, metric!, metric?, option, options, #run, #shell, subclasses
Constructor Details
This class inherits a constructor from Perus::Pinger::Command
Instance Method Details
#execute(commands, &message_callback) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/perus/pinger/chrome_command.rb', line 17 def execute(commands, &) # discover the first page shown in chrome tries = 0 @page = nil begin pages = JSON.parse(RestClient.get("http://#{.host}:#{.port}/json")) pages.reject! {|page| page['url'].include?('chrome-extension')} @page = pages.first rescue Errno::ECONNREFUSED, Errno::ECONNRESET => e tries += 1 sleep 3 retry if tries < 4 return end return if @page.nil? EM.run do @ws = Faye::WebSocket::Client.new(@page['webSocketDebuggerUrl']) @ws.on :error do |event| puts "Chrome error: #{event}" EM.stop_event_loop end @ws.on :close do |event| EM.stop_event_loop end @ws.on :message do |event| if block_given? json = JSON.parse(event.data) .call(json) end end # send each command, responses will appear commands.each do |command| send_command(command) end # cutoff console message loading after N seconds EM.add_timer(.timeout_seconds) do @ws.close end end end |
#send_command(command) ⇒ Object
13 14 15 |
# File 'lib/perus/pinger/chrome_command.rb', line 13 def send_command(command) @ws.send(command) end |