Use livereload properly. Fix #10

This commit is contained in:
Vincent Prouillet 2017-05-12 21:15:50 +09:00
parent aee1552619
commit c791dffae5
3 changed files with 35 additions and 45 deletions

10
Cargo.lock generated
View file

@ -68,7 +68,7 @@ name = "backtrace-sys"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.45 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.46 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -173,7 +173,7 @@ name = "cmake"
version = "0.1.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.45 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.46 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -257,7 +257,7 @@ dependencies = [
[[package]]
name = "gcc"
version = "0.3.45"
version = "0.3.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -408,7 +408,7 @@ name = "miniz-sys"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"gcc 0.3.45 (registry+https://github.com/rust-lang/crates.io-index)",
"gcc 0.3.46 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.22 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1096,7 +1096,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum fnv 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "6cc484842f1e2884faf56f529f960cc12ad8c71ce96cc7abba0a067c98fee344"
"checksum fsevent 0.2.16 (registry+https://github.com/rust-lang/crates.io-index)" = "dfe593ebcfc76884138b25426999890b10da8e6a46d01b499d7c54c604672c38"
"checksum fsevent-sys 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "1a772d36c338d07a032d5375a36f15f9a7043bf0cb8ce7cee658e037c6032874"
"checksum gcc 0.3.45 (registry+https://github.com/rust-lang/crates.io-index)" = "40899336fb50db0c78710f53e87afc54d8c7266fb76262fecc78ca1a7f09deae"
"checksum gcc 0.3.46 (registry+https://github.com/rust-lang/crates.io-index)" = "181e3cebba1d663bd92eb90e2da787e10597e027eb00de8d742b260a7850948f"
"checksum getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9047cfbd08a437050b363d35ef160452c5fe8ea5187ae0a624708c91581d685"
"checksum glob 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8be18de09a56b60ed0edf84bc9df007e30040691af7acd1c41874faac5895bfb"
"checksum httparse 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "77f756bed9ee3a83ce98774f4155b42a31b787029013f3a7d83eca714e500e21"

View file

@ -4,7 +4,7 @@
_ref = require('./protocol'), Parser = _ref.Parser, PROTOCOL_6 = _ref.PROTOCOL_6, PROTOCOL_7 = _ref.PROTOCOL_7;
Version = '2.2.2';
Version = '2.2.1';
exports.Connector = Connector = (function() {
function Connector(options, WebSocket, Timer, handlers) {
@ -12,7 +12,7 @@
this.WebSocket = WebSocket;
this.Timer = Timer;
this.handlers = handlers;
this._uri = "ws" + (this.options.https ? "s" : "") + "://" + this.options.host + ":" + this.options.port + "/livereload";
this._uri = "ws://" + this.options.host + ":" + this.options.port + "/livereload";
this._nextDelay = this.options.mindelay;
this._connectionDesired = false;
this.protocol = 0;
@ -278,8 +278,7 @@
},{}],4:[function(require,module,exports){
(function() {
var Connector, LiveReload, Options, Reloader, Timer,
__hasProp = {}.hasOwnProperty;
var Connector, LiveReload, Options, Reloader, Timer;
Connector = require('./connector').Connector;
@ -291,15 +290,11 @@
exports.LiveReload = LiveReload = (function() {
function LiveReload(window) {
var k, v, _ref;
this.window = window;
this.listeners = {};
this.plugins = [];
this.pluginIdentifiers = {};
this.console = this.window.console && this.window.console.log && this.window.console.error ? this.window.location.href.match(/LR-verbose/) ? this.window.console : {
log: function() {},
error: this.window.console.error.bind(this.window.console)
} : {
this.console = this.window.location.href.match(/LR-verbose/) && this.window.console && this.window.console.log && this.window.console.error ? this.window.console : {
log: function() {},
error: function() {}
};
@ -307,20 +302,9 @@
this.console.error("LiveReload disabled because the browser does not seem to support web sockets");
return;
}
if ('LiveReloadOptions' in window) {
this.options = new Options();
_ref = window['LiveReloadOptions'];
for (k in _ref) {
if (!__hasProp.call(_ref, k)) continue;
v = _ref[k];
this.options.set(k, v);
}
} else {
this.options = Options.extract(this.window.document);
if (!this.options) {
this.console.error("LiveReload disabled because it could not find its own <SCRIPT> tag");
return;
}
if (!(this.options = Options.extract(this.window.document))) {
this.console.error("LiveReload disabled because it could not find its own <SCRIPT> tag");
return;
}
this.reloader = new Reloader(this.window, this.console, Timer);
this.connector = new Connector(this.options, this.WebSocket, Timer, {
@ -342,7 +326,16 @@
})(this),
error: (function(_this) {
return function(e) {
return console.log("" + e.message + ".");
console.log(e);
// if (e instanceof ProtocolError) {
// if (typeof console !== "undefined" && console !== null) {
// return console.log("" + e.message + ".");
// }
// } else {
// if (typeof console !== "undefined" && console !== null) {
// return console.log("LiveReload internal error: " + e.message);
// }
// }
};
})(this),
disconnected: (function(_this) {
@ -380,7 +373,6 @@
};
})(this)
});
this.initialized = true;
}
LiveReload.prototype.on = function(eventName, handler) {
@ -409,9 +401,6 @@
LiveReload.prototype.shutDown = function() {
var _base;
if (!this.initialized) {
return;
}
this.connector.disconnect();
this.log("LiveReload disconnected.");
return typeof (_base = this.listeners).shutdown === "function" ? _base.shutdown() : void 0;
@ -423,9 +412,6 @@
LiveReload.prototype.addPlugin = function(pluginClass) {
var plugin;
if (!this.initialized) {
return;
}
if (this.hasPlugin(pluginClass.identifier)) {
return;
}
@ -448,9 +434,6 @@
LiveReload.prototype.analyze = function() {
var plugin, pluginData, pluginsData, _i, _len, _ref;
if (!this.initialized) {
return;
}
if (!(this.connector.protocol >= 7)) {
return;
}
@ -480,7 +463,6 @@
exports.Options = Options = (function() {
function Options() {
this.https = false;
this.host = null;
this.port = 35729;
this.snipver = null;
@ -512,7 +494,6 @@
element = _ref[_i];
if ((src = element.src) && (m = src.match(/^[^:]+:\/\/(.*)\/z?livereload\.js(?:\?(.*))?$/))) {
options = new Options();
options.https = src.indexOf("https") === 0;
if (mm = m[1].match(/^([^\/:]+)(?::(\d+))?$/)) {
options.host = mm[1];
if (mm[2]) {
@ -561,7 +542,7 @@
}
Parser.prototype.reset = function() {
return this.protocol = 7;
return this.protocol = null;
};
Parser.prototype.process = function(data) {

View file

@ -9,7 +9,7 @@ use iron::{Iron, Request, IronResult, Response, status};
use mount::Mount;
use staticfile::Static;
use notify::{Watcher, RecursiveMode, watcher};
use ws::{WebSocket, Sender};
use ws::{WebSocket, Sender, Message};
use gutenberg::Site;
use gutenberg::errors::{Result, ResultExt};
@ -93,8 +93,17 @@ pub fn serve(interface: &str, port: &str, config_file: &str) -> Result<()> {
let _iron = Iron::new(mount).http(address.as_str()).unwrap();
// The websocket for livereload
let ws_server = WebSocket::new(|_| {
|_| {
let ws_server = WebSocket::new(|output: Sender| {
move |msg: Message| {
if msg.into_text().unwrap().contains("\"hello\"") {
return output.send(Message::text(r#"
{
"command": "hello",
"protocols": [ "http://livereload.com/protocols/official-7" ],
"serverName": "Gutenberg"
}
"#));
}
Ok(())
}
}).unwrap();