Adds the live_reload_port option.

This allows the javascript to work with a non default value for the
livereload port (specified with the port option in the guard command).

For example, in config/environments/development.rb in rails:
  config.middleware.insert_after(ActionDispatch::Static, Rack::LiveReload, :live_reload_port => 22351)
works with the following in the Guardfile:
  guard 'livereload', port: '22351' do
    ...
  end

This allows multiple guard livereload processes to operate at the same time without
conflicts.
This commit is contained in:
David Miani 2013-02-03 22:13:48 +11:00
parent 85855cb25a
commit 206ce04e77
4 changed files with 32 additions and 6 deletions

View File

@ -317,7 +317,7 @@ var Options;
__options.Options = Options = (function() { __options.Options = Options = (function() {
function Options() { function Options() {
this.host = null; this.host = null;
this.port = 35729; this.port = RACK_LIVERELOAD_PORT;
this.snipver = null; this.snipver = null;
this.ext = null; this.ext = null;
this.extver = null; this.extver = null;

View File

@ -3,15 +3,19 @@ require 'erb'
module Rack module Rack
class LiveReload class LiveReload
LIVERELOAD_JS_PATH = '/__rack/livereload.js' LIVERELOAD_JS_PATH = '/__rack/livereload.js'
LIVERELOAD_LOCAL_URI = 'http://localhost:35729/livereload.js'
HEAD_TAG_REGEX = /<head>|<head[^(er)][^<]*>/ HEAD_TAG_REGEX = /<head>|<head[^(er)][^<]*>/
BAD_USER_AGENTS = [ %r{MSIE} ] BAD_USER_AGENTS = [ %r{MSIE} ]
def livereload_local_uri
"http://localhost:#{@port}/livereload.js"
end
attr_reader :app attr_reader :app
def initialize(app, options = {}) def initialize(app, options = {})
@app, @options = app, options @app, @options = app, options
@port = @options[:live_reload_port] || 35729
end end
def use_vendored? def use_vendored?
@ -23,7 +27,8 @@ module Rack
require 'net/http' require 'net/http'
require 'uri' require 'uri'
uri = URI.parse(LIVERELOAD_LOCAL_URI)
uri = URI.parse(livereload_local_uri)
http = Net::HTTP.new(uri.host, uri.port) http = Net::HTTP.new(uri.host, uri.port)
http.read_timeout = 1 http.read_timeout = 1
@ -71,7 +76,7 @@ module Rack
if use_vendored? if use_vendored?
src = LIVERELOAD_JS_PATH.dup + "?host=#{host_to_use}" src = LIVERELOAD_JS_PATH.dup + "?host=#{host_to_use}"
else else
src = LIVERELOAD_LOCAL_URI.dup.gsub('localhost', host_to_use) + '?' src = livereload_local_uri.dup.gsub('localhost', host_to_use) + '?'
end end
src << "&mindelay=#{@options[:min_delay]}" if @options[:min_delay] src << "&mindelay=#{@options[:min_delay]}" if @options[:min_delay]

View File

@ -4,6 +4,7 @@
<% if force_swf? %> <% if force_swf? %>
WEB_SOCKET_FORCE_FLASH = true; WEB_SOCKET_FORCE_FLASH = true;
<% end %> <% end %>
RACK_LIVERELOAD_PORT = <%= @port %>;
</script> </script>
<script type="text/javascript" src="/__rack/swfobject.js"></script> <script type="text/javascript" src="/__rack/swfobject.js"></script>
<script type="text/javascript" src="/__rack/web_socket.js"></script> <script type="text/javascript" src="/__rack/web_socket.js"></script>

View File

@ -12,7 +12,7 @@ describe Rack::LiveReload do
let(:env) { {} } let(:env) { {} }
let(:options) { {} } let(:options) { {} }
describe described_class::LIVERELOAD_LOCAL_URI do describe "livereload local uri" do
context 'does not exist' do context 'does not exist' do
before do before do
stub_request(:any, 'localhost:35729/livereload.js').to_timeout stub_request(:any, 'localhost:35729/livereload.js').to_timeout
@ -29,6 +29,16 @@ describe Rack::LiveReload do
it { should_not use_vendored } it { should_not use_vendored }
end end
context 'with custom port' do
let(:options) { {:live_reload_port => '12348'}}
context 'exists' do
before do
stub_request(:any, 'localhost:12348/livereload.js')
end
it { should_not use_vendored }
end
end
context 'specify vendored' do context 'specify vendored' do
let(:options) { { :source => :vendored } } let(:options) { { :source => :vendored } }
@ -120,8 +130,18 @@ describe Rack::LiveReload do
body_dom.at_css("script:eq(4)")[:src].should include(described_class::LIVERELOAD_JS_PATH) body_dom.at_css("script:eq(4)")[:src].should include(described_class::LIVERELOAD_JS_PATH)
body_dom.at_css("script:last-child")[:insert].should == "before" body_dom.at_css("script:last-child")[:insert].should == "before"
end end
end end
describe "LIVERELOAD_PORT value" do
let(:options) { {:live_reload_port => 12345 }}
it "sets the variable at the top of the file" do
body.should include 'RACK_LIVERELOAD_PORT = 12345'
end
end
context 'in header tags' do context 'in header tags' do
let(:page_html) { "<header class='hero'><h1>Just a normal header tag</h1></header>" } let(:page_html) { "<header class='hero'><h1>Just a normal header tag</h1></header>" }
@ -140,7 +160,7 @@ describe Rack::LiveReload do
it 'should add the LR livereload js script tag' do it 'should add the LR livereload js script tag' do
body.should include("script") body.should include("script")
body.should include(described_class::LIVERELOAD_LOCAL_URI.gsub('localhost', 'host')) body.should include(middleware.livereload_local_uri.gsub('localhost', 'host'))
end end
end end