Sha256: 8b0cb4a0fa555c75f625bac53efb5ee632c23fd2c3afb9990dc122f450234c7c

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

require 'em-websocket'
require 'json'

module Guard
  class LiveReload
    class Reactor
      
      attr_reader :thread, :web_sockets
      
      def initialize(options)
        @web_sockets = []
        @options     = options
        @thread      = start_threaded_reactor(options)
      end
      
      def stop
        thread.kill
      end
      
      def reload_browser(paths = [])
        UI.info "Reloading browser: #{paths.join(' ')}"
        paths.each do |path|
          data = ['refresh', { :path => path, :apply_js_live => @options[:apply_js_live], :apply_css_live => @options[:apply_css_live] }].to_json
          UI.debug data
          @web_sockets.each { |ws| ws.send(data) }
        end
      end
      
    private
      
      def start_threaded_reactor(options)
        Thread.new do
          EventMachine.run do
            UI.info "LiveReload #{options[:api_version]} is waiting for a browser to connect."
            EventMachine::WebSocket.start(:host => options[:host], :port => options[:port]) do |ws|
              ws.onopen do
                begin
                  UI.info "Browser connected."
                  ws.send "!!ver:#{options[:api_version]}"
                  @web_sockets << ws
                rescue
                  UI.errror $!
                  UI.errror $!.backtrace
                end
              end
              
              ws.onmessage do |msg|
                UI.info "Browser URL: #{msg}"
              end
              
              ws.onclose do
                @web_sockets.delete ws
                UI.info "Browser disconnected."
              end
            end
          end
        end
      end
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
guard-livereload-0.1.5 lib/guard/livereload/reactor.rb