class Jekyll::Livereload::Reactor
Attributes
opts[R]
thread[R]
Public Class Methods
new(opts)
click to toggle source
# File lib/jekyll-livereload/websocket.rb, line 83 def initialize(opts) @opts = opts @thread = nil @websockets = [] @connections_count = 0 end
Public Instance Methods
reload()
click to toggle source
For a description of the protocol see feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol
# File lib/jekyll-livereload/websocket.rb, line 124 def reload Livereload.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 Livereload.pages.clear end
running?()
click to toggle source
# File lib/jekyll-livereload/websocket.rb, line 95 def running? !@thread.nil? && @thread.alive? end
start()
click to toggle source
# File lib/jekyll-livereload/websocket.rb, line 99 def start @thread = Thread.new do # Use epoll if the kernel supports it EM.epoll EM.run do protocol = @opts[:secure] ? "https" : "http" Jekyll.logger.info("LiveReload Server:", "#{protocol}://#{@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 end end end
stop()
click to toggle source
# File lib/jekyll-livereload/websocket.rb, line 90 def stop @thread.kill unless @thread.nil? Jekyll.logger.debug("LiveReload Server:", "halted") end
Private Instance Methods
connect(ws, _handshake)
click to toggle source
# File lib/jekyll-livereload/websocket.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/jekyll-livereload/websocket.rb, line 158 def disconnect(ws) @websockets.delete(ws) end
print_message(json_message)
click to toggle source
# File lib/jekyll-livereload/websocket.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