Class: Hawkins::LiveReloadReactor

Inherits:
Object
  • Object
show all
Defined in:
lib/hawkins/websockets.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLiveReloadReactor

Returns a new instance of LiveReloadReactor.



72
73
74
75
76
77
78
# 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

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



69
70
71
# File 'lib/hawkins/websockets.rb', line 69

def opts
  @opts
end

#reactor_mutexObject (readonly)

Returns the value of attribute reactor_mutex.



70
71
72
# File 'lib/hawkins/websockets.rb', line 70

def reactor_mutex
  @reactor_mutex
end

#reactor_running_condObject (readonly)

Returns the value of attribute reactor_running_cond.



70
71
72
# File 'lib/hawkins/websockets.rb', line 70

def reactor_running_cond
  @reactor_running_cond
end

#threadObject (readonly)

Returns the value of attribute thread.



68
69
70
# File 'lib/hawkins/websockets.rb', line 68

def thread
  @thread
end

Instance Method Details

#connect(ws, _handshake) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
# 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) ⇒ Object



158
159
160
# File 'lib/hawkins/websockets.rb', line 158

def disconnect(ws)
  @websockets.delete(ws)
end


162
163
164
165
166
# 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) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# 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?Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/hawkins/websockets.rb', line 85

def running?
  EM.reactor_running?
end

#start(opts) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# 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

#stopObject



80
81
82
83
# File 'lib/hawkins/websockets.rb', line 80

def stop
  EM.stop if EM.reactor_running?
  Jekyll.logger.debug("LiveReload Server:", "halted")
end