Sha256: 4b7e9aaeb283f4d23ae2a3b4b6c36ac48174ff51a11a16447ba5109d566b1f1d

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

require 'em-websocket'
require 'multi_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 = MultiJson.encode(['refresh', {
            :path           => "#{Dir.pwd}/#{path}",
            :apply_js_live  => @options[:apply_js_live],
            :apply_css_live => @options[:apply_css_live]
          }])
          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.start_server(options[:host], options[:port], EventMachine::WebSocket::Connection, {}) 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}"  if msg =~ /^(https?|file):/
              end

              ws.onclose do
                @web_sockets.delete ws
                UI.info "Browser disconnected."
              end
            end
          end
        end
      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guard-livereload-1.1.3 lib/guard/livereload/reactor.rb
guard-livereload-1.1.2 lib/guard/livereload/reactor.rb
guard-livereload-1.1.1 lib/guard/livereload/reactor.rb
guard-livereload-1.1.0 lib/guard/livereload/reactor.rb