Class: Slinky::LiveReload

Inherits:
Object
  • Object
show all
Defined in:
lib/slinky/live_reload.rb

Overview

Code to interface with the LiveReload set of browser plugins and javascript clients. Adapted from the guard-livereload project at github.com/guard/guard-livereload.

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ LiveReload

Returns a new instance of LiveReload.



6
7
8
9
10
# File 'lib/slinky/live_reload.rb', line 6

def initialize host, port
  @host = host
  @port = port
  @websockets = []
end

Instance Method Details

#reload_browser(paths = []) ⇒ Object



38
39
40
41
42
43
44
45
46
47
# File 'lib/slinky/live_reload.rb', line 38

def reload_browser(paths = [])
  paths.each do |path|
    data = MultiJson.encode(['refresh', {
                               :path           => "#{path}",
                               :apply_js_live  => false,
                               :apply_css_live => true
                             }])
    @websockets.each { |ws| ws.send(data) }
  end
end

#runObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/slinky/live_reload.rb', line 12

def run
  EventMachine.run do
    begin
      EventMachine.start_server(@host,
                                @port,
                                EventMachine::WebSocket::Connection, {}) do |ws|
        ws.onopen do
          begin
            ws.send "!!ver:1.6"
            @websockets << ws
          rescue
            $stderr.puts $!.to_s.foreground(:red)
          end
        end

        ws.onclose do
          @websockets.delete ws
        end
      end
      $stdout.puts "Started live-reload server on port #{@port}"
    rescue
      puts "Unable to start livereload server on port #{@port}".foreground(:red)
    end
  end
end