Class: Jekyll::Livereload::Reactor
- Inherits:
-
Object
- Object
- Jekyll::Livereload::Reactor
- Defined in:
- lib/jekyll-livereload/websocket.rb
Instance Attribute Summary collapse
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Instance Method Summary collapse
-
#initialize(opts) ⇒ Reactor
constructor
A new instance of Reactor.
-
#reload ⇒ Object
For a description of the protocol see feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol.
- #running? ⇒ Boolean
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(opts) ⇒ Reactor
Returns a new instance of Reactor.
83 84 85 86 87 88 89 |
# File 'lib/jekyll-livereload/websocket.rb', line 83 def initialize(opts) @opts = opts @thread = nil @websockets = [] @connections_count = 0 trap("INT") { stop } end |
Instance Attribute Details
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
81 82 83 |
# File 'lib/jekyll-livereload/websocket.rb', line 81 def opts @opts end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
80 81 82 |
# File 'lib/jekyll-livereload/websocket.rb', line 80 def thread @thread end |
Instance Method Details
#reload ⇒ Object
For a description of the protocol see feedback.livereload.com/knowledgebase/articles/86174-livereload-protocol
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/jekyll-livereload/websocket.rb', line 125 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? ⇒ Boolean
96 97 98 |
# File 'lib/jekyll-livereload/websocket.rb', line 96 def running? !@thread.nil? && @thread.alive? end |
#start ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/jekyll-livereload/websocket.rb', line 100 def start @thread = Thread.new do # Use epoll if the kernel supports it EM.epoll # TODO enable SSL 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. do |msg| (msg) end end end end end |
#stop ⇒ Object
91 92 93 94 |
# File 'lib/jekyll-livereload/websocket.rb', line 91 def stop @thread.kill unless @thread.nil? Jekyll.logger.debug("LiveReload Server:", "halted") end |