Sha256: 3bb7ef5d9679936d7144d7a965cbf20aa41dbf25530a60c885b4e83809b48cd7
Contents?: true
Size: 1.91 KB
Versions: 4
Compression:
Stored size: 1.91 KB
Contents
require 'multi_json' module Guard class LiveReload class Reactor attr_reader :web_sockets, :thread, :options def initialize(options) @web_sockets = [] @options = options @thread = Thread.new { _start_reactor } end def stop thread.kill end def reload_browser(paths = []) UI.info "Reloading browser: #{paths.join(' ')}" paths.each do |path| data = _data(path) UI.debug(data) web_sockets.each { |ws| ws.send(MultiJson.encode(data)) } end end private def _data(path) data = { command: 'reload', path: "#{Dir.pwd}/#{path}", liveCSS: options[:apply_css_live] } if options[:override_url] && File.exist?(path) data[:overrideURL] = '/' + path end data end def _start_reactor EventMachine.epoll EventMachine.run do EventMachine.start_server(options[:host], options[:port], WebSocket, {}) do |ws| ws.onopen { _connect(ws) } ws.onclose { _disconnect(ws) } ws.onmessage { |msg| _print_message(msg) } end UI.info "LiveReload is waiting for a browser to connect." end end def _connect(ws) UI.info "Browser connected." ws.send MultiJson.encode( command: 'hello', protocols: ['http://livereload.com/protocols/official-7'], serverName: 'guard-livereload' ) @web_sockets << ws rescue UI.error $! UI.error $!.backtrace end def _disconnect(ws) UI.info "Browser disconnected." @web_sockets.delete(ws) end def _print_message(message) message = MultiJson.decode(message) UI.info "Browser URL: #{message['url']}" if message['command'] == 'url' end end end end
Version data entries
4 entries across 4 versions & 1 rubygems