class Hawkins::LiveReloadReactor
Attributes
opts[R]
reactor_mutex[R]
reactor_running_cond[R]
thread[R]
Public Class Methods
new()
click to toggle source
# File lib/hawkins/websockets.rb, line 72 def initialize @thread = nil @websockets = [] @connections_count = 0 @reactor_mutex = Mutex.new @reactor_running_cond = ConditionVariable.new end
Public Instance Methods
connect(ws, _handshake)
click to toggle source
# File lib/hawkins/websockets.rb, line 145 def connect(ws, _handshake) @connections_count += 1 Jekyll.logger.info("LiveReload:", "Browser connected") if @connections_count == 1 ws.send( JSON.dump( :command => 'hello', :protocols => ['http://livereload.com/protocols/official-7'], :serverName => 'jekyll livereload', )) @websockets << ws end
disconnect(ws)
click to toggle source
# File lib/hawkins/websockets.rb, line 158 def disconnect(ws) @websockets.delete(ws) end
print_message(json_message)
click to toggle source
# File lib/hawkins/websockets.rb, line 162 def print_message(json_message) msg = JSON.parse(json_message) # Not sure what the 'url' command even does in LiveReload Jekyll.logger.info("LiveReload:", "Browser URL: #{msg['url']}") if msg['command'] == 'url' end
reload(pages)
click to toggle source
For a description of the protocol see feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol
# File lib/hawkins/websockets.rb, line 127 def reload(pages) pages.each do |p| msg = { :command => 'reload', :path => p.path, :liveCSS => true, } # TODO Add support for override URL? # See http://feedback.livereload.com/knowledgebase/articles/86220-preview-css-changes-against-a-live-site-then-uplo Jekyll.logger.debug("LiveReload:", "Reloading #{p.path}") @websockets.each do |ws| ws.send(JSON.dump(msg)) end end end
running?()
click to toggle source
# File lib/hawkins/websockets.rb, line 85 def running? EM.reactor_running? end
start(opts)
click to toggle source
# File lib/hawkins/websockets.rb, line 89 def start(opts) @thread = Thread.new do # Use epoll if the kernel supports it EM.epoll EM.run do Jekyll.logger.info("LiveReload Server:", "#{opts['host']}:#{opts['reload_port']}") EM.start_server(opts['host'], opts['reload_port'], HttpAwareConnection, opts) do |ws| ws.onopen do |handshake| connect(ws, handshake) end ws.onclose do disconnect(ws) end ws.onmessage do |msg| print_message(msg) end end # Notify blocked threads that EventMachine has started or shutdown EM.schedule do @reactor_mutex.synchronize do @reactor_running_cond.broadcast end end EM.add_shutdown_hook do @reactor_mutex.synchronize do @reactor_running_cond.broadcast end end end end @thread.abort_on_exception = true end
stop()
click to toggle source
# File lib/hawkins/websockets.rb, line 80 def stop EM.stop if EM.reactor_running? Jekyll.logger.debug("LiveReload Server:", "halted") end