Skip to content

Commit

Permalink
Added 'reload' task, along with node server support, Issue tbranyen#10
Browse files Browse the repository at this point in the history
  • Loading branch information
webxl committed Jan 13, 2012
1 parent 9c5f633 commit 3a12b54
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 2 deletions.
8 changes: 6 additions & 2 deletions build/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ config.init({

watch: {
files: ["assets/**/*", "app/**/*"],
tasks: "lint:files concat jst",
tasks: "lint:files concat jst reload",

min: {
files: ["assets/**/*", "app/**/*"],
Expand All @@ -50,7 +50,11 @@ config.init({

clean: {
folder: "dist/"
}
},

reload: {
dummy: "grunt requires this why?"
}

});

Expand Down
33 changes: 33 additions & 0 deletions build/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var fs = require("fs");
var express = require("express");
var site = express.createServer();
var WebSocketServer = require('websocket').server

// Determine which dist directory to use
var dir = process.argv.length > 2 && "./dist/" + process.argv[2];
Expand All @@ -18,6 +19,38 @@ site.use("/dist", express.static("./dist"));
// Serve favicon.ico
site.use(express.favicon("./favicon.ico"));

if (process.argv[2] == '--reload' || process.argv.length > 3 && process.argv[3] == '--reload') {

console.log("Reload task support enabled.");

wsServer = new WebSocketServer({
httpServer: site,
autoAcceptConnections: true // DON'T use on production!
});

wsServer.on('connect', function() {
console.log((new Date()) + ' Connection accepted.');
});

site.post("/reload", function(req, res) {
var fileData = req.data,
connections = wsServer.connections;

for (var i=0;i<connections.length;i++) {
connections[i].sendUTF('reload');
}
});

// Todo: figure out a better way to handle this
site.get("/reload.js", function(req, res) {
fs.createReadStream("./build/tasks/reload/client.js").pipe(res);
});
} else {
site.get("/reload.js", function(req, res) {
res.send("// to enable, pass '--reload' to server command");
});
}

// Ensure all routes go home, client side app..
site.get("*", function(req, res) {
fs.createReadStream("./index.html").pipe(res);
Expand Down
40 changes: 40 additions & 0 deletions build/tasks/reload/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Reload Task Client script
*
* Created by webxl
* Date: 1/11/12
*
*/

$(function() {

var reloader = function Reloader() {

var url = 'ws://localhost:8000';

return {
isSocketConnected:function () {
return this.socket && this.socket.readyState === WebSocket.OPEN;
},
connect:function () {
if (this.isSocketConnected()) {
return;
}
if (this.connectTimeout) {
clearTimeout(this.connectTimeout);
}
this.socket = new WebSocket(url);

this.socket.onmessage = function (msg) {
this.close();
window.document.location.reload();
}
// Todo: reconnect support
}
};

}();

setTimeout(function() { reloader.connect(); }, 3000);

});
49 changes: 49 additions & 0 deletions build/tasks/reload/reload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// ============================================================================
// TASKS
// ============================================================================

task.registerBasicTask("reload", "Reload connected clients.", function (data, name) {
// Minify CSS.
var errorcount = fail.errorcount;
var files = file.expand(data);
file.write(name, task.helper('reload', files));

// Fail task if there were errors.
if (fail.errorcount > errorcount) {
return false;
}

// Otherwise, print a success message.
log.writeln("File \"" + name + "\" created.");
});

// ============================================================================
// HELPERS
// ============================================================================

task.registerHelper("reload", function (files) {
var http = require('http');

var fileData = JSON.stringify(files);

var options = {
host:'localhost',
port:8000,
method:'POST',
path:'/reload',
headers:{
'Content-Type':'application/x-www-form-urlencoded',
'Content-Length':fileData.length
}
};

var post_req = http.request(options,
function (proxyResponse) {}).on('error', function (e) {
console.log("Got error: " + e.message);
});

post_req.write(fileData);
post_req.end();

});

3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
<script src="/assets/js/templates.js"></script>
<script src="/assets/js/app.js"></script>
-->

<script src="/reload.js"></script>

</body>
</html>

0 comments on commit 3a12b54

Please sign in to comment.