diff --git a/examples/acequiaClient.html b/examples/acequiaClient.html
index e3f659a..0f620c3 100644
--- a/examples/acequiaClient.html
+++ b/examples/acequiaClient.html
@@ -4,8 +4,7 @@
Acequia Client Test Page
-
-
+
diff --git a/lib/acequia.js b/lib/acequia.js
index 2abd6de..7567f10 100644
--- a/lib/acequia.js
+++ b/lib/acequia.js
@@ -3,20 +3,25 @@
// TODO: Listen with http server and serve up the acequia client js file.
-// TODO: At startup compress the acequia client js file, using uglify.
-
-
// Imports and globals.
var http = require("http"),
url = require("url"),
net = require("net"),
+ io = require('socket.io'),
log4js = require('log4js-node'),
dgram = require("dgram"),
osc = require("./osc.js"),
ac = require("./client.js"),
msg = require("./msg.js"),
querystring = require("querystring"),
- Buffer = require('buffer').Buffer;
+ Buffer = require('buffer').Buffer,
+ fs = require("fs"),
+ URL = require("url"),
+ genclient = require("./genclient");
+
+
+// Generate the client code
+genclient.generateClientCode();
var INTERNAL_IP = "0.0.0.0",
OSC_PORT = 9090,
@@ -27,7 +32,7 @@ var INTERNAL_IP = "0.0.0.0",
// The list of clients
var clients = new ac.AcequiaClients(TIMEOUT * 1000);
-var oscServer, wsServer, tcpServer;
+var oscServer, wsServer, tcpServer, httpServer;
var logger = log4js.getLogger("acequia");
@@ -99,7 +104,36 @@ function startServers() {
oscServer.bind(OSC_PORT, INTERNAL_IP);
// Websocket server:
- wsServer = require('socket.io').listen(WS_PORT);
+
+ // TODO: Read into memory in genclient
+ var serveClientCode = function (res, min) {
+ var file, fd, stats;
+ min = min ? ".min" : "";
+ file = process.cwd() + "/lib/dist/acequia" + min + ".js";
+ stats = fs.statSync(file);
+ buffer = new Buffer(stats.size);
+
+ fd = fs.openSync(file, "r");
+ fs.readSync(fd, buffer, 0, stats.size, null);
+ fs.close(fd);
+
+ res.writeHead(200,{ 'Content-Type': 'text/javascript' });
+ res.end(buffer.toString("utf8"));
+ };
+
+ httpServer = http.createServer(function (req, res){
+ var pathName = URL.parse(req.url, true).pathname;
+ console.log("HTTP server received " + pathName);
+
+ if (pathName == "/acequia/acequia.js") {
+ serveClientCode(res, false);
+ } else if (pathName == "/acequia/acequia.min.js") {
+ serveClientCode(res, true);
+ }
+ });
+ httpServer.listen(WS_PORT);
+
+ wsServer = io.listen(httpServer);
wsServer.enable('browser client minification'); // send minified client
wsServer.enable('browser client etag'); // apply etag caching logic based on version number
@@ -164,7 +198,7 @@ function startServers() {
logger.debug("TCP: %s:%s connect", socket.remoteAddress, socket.remotePort);
});
- socket.addListener("data", function(data) {
+ socket.addListener("data", function (data) {
var index = 0, msgs = [], size, message,
buffer = new Buffer(data);
@@ -231,7 +265,7 @@ function startServers() {
});
});
- tcpServer.on("listening", function() {
+ tcpServer.on("listening", function () {
logger.debug("TCP Server is listening on %j", tcpServer.address());
});
diff --git a/lib/genclient.js b/lib/genclient.js
index 574b664..4675bee 100644
--- a/lib/genclient.js
+++ b/lib/genclient.js
@@ -1,49 +1,74 @@
+/*globals require exports process*/
+
var jsp = require("uglify-js").parser;
var pro = require("uglify-js").uglify;
var fs = require("fs");
var util = require("util");
var Buffer = require('buffer').Buffer;
-var concatenateClientFiles = function (distDir) {
- var buffers = [], uber, fd, stats, i, size = 0, file, files = ["msg.js", "client/acequiaClient.js"];
+var fullCode = "";
+var minifiedCode = "";
+
+/**
+ * Reads the client-side files and concatenate them into a single file
+ * @returns {String} the concatenated file
+ */
+var concatenateClientFiles = function () {
+ var buffer, code = "", fd, stats, i, size = 0, file, files = ["msg.js", "client/acequiaClient.js"];
for (i in files) {
file = process.cwd() + "/lib/" + files[i];
stats = fs.statSync(file);
- buffers.push(new Buffer(stats.size));
+ buffer = new Buffer(stats.size);
fd = fs.openSync(file, "r");
- size += fs.readSync(fd, buffers[i], 0, stats.size, null);
+ size += fs.readSync(fd, buffer, 0, stats.size, null);
fs.close(fd);
+
+ code += buffer.toString("utf8");
}
- uber = "";
- for (i in buffers) {
- uber += buffers[i].toString("utf8");
- }
+ return code;
+};
+
+/**
+ * Write the code generated in concatenateClientFiles to two files:
+ * one as is, the other minified.
+ * @param {String} code The code to write out.
+ * @param {String} distDir The directories where the files should be written.
+ */
+var writeClientFiles = function (code, distDir) {
+ var fd, ast;
+ // Write the non-minified version
fd = fs.openSync(distDir + "acequia.js", "w");
- fs.writeSync(fd, uber, 0);
+ fs.writeSync(fd, code, 0);
fs.close(fd);
-
- return uber;
-};
-var minifyClientFiles = function (code, distDir) {
- var ast = jsp.parse(code);
+ // Minify the code
+ ast = jsp.parse(code);
ast = pro.ast_mangle(ast);
ast = pro.ast_squeeze(ast);
- var final_code = pro.gen_code(ast);
+ minifiedCode = pro.gen_code(ast);
+ // Write the minified version
fd = fs.openSync(distDir + "acequia.min.js", "w");
- fs.writeSync(fd, final_code, 0);
+ fs.writeSync(fd, minifiedCode, 0);
fs.close(fd);
};
+/**
+ * Generates the client code for the server to serve up.
+ */
exports.generateClientCode = function () {
- var distDir = process.cwd() + "/lib/dist/";
+ var distDir;
+
+ distDir = process.cwd() + "/lib/dist/";
fs.mkdir(distDir, 0777);
- var code = concatenateClientFiles(distDir);
- minifyClientFiles(code, distDir);
-};
\ No newline at end of file
+ fullCode = concatenateClientFiles();
+ writeClientFiles(fullCode, distDir);
+};
+
+exports.code = fullCode;
+exports.minifiedCode = minifiedCode;
\ No newline at end of file
diff --git a/package.json b/package.json
index 3ca22d5..e32e53e 100644
--- a/package.json
+++ b/package.json
@@ -1,8 +1,10 @@
-{ "description":"Message router for node.js supporting multiple protocols"
-, "directories":{"doc":"./doc","lib":"./lib/ws"}
-, "engines":{"node":">=0.2.0-0"}
-, "name" : "Acequia"
-, "repository":{"type":"git","url":"http://github.com/miksago/node-websocket-server.git"}
-, "scripts": { "start": "node ./acequia.js" }
-, "version" : "0.5.0"
+{
+"name" : "Acequia"
+,"description":"Message router for node.js supporting multiple protocols"
+, "main": "index"
+, "directories": {"lib": "./lib"}
+, "engines":{"node":">=0.6.0-0"}
+, "repository":{"type":"git","url":"https://prgsmall@github.com/prgsmall/acequia.git"}
+, "scripts": { "start": "node ." }
+, "version" : "0.1.0"
}