From 3fe9022d3e168a6467b760f29b73b7edb6aa0774 Mon Sep 17 00:00:00 2001 From: Mattias Erming Date: Tue, 4 Mar 2014 13:50:43 -0800 Subject: [PATCH] Added the Event model --- app.js | 2 +- client/index.html | 4 ++-- client/js/client.js | 44 ++++++++++++++++++++++++++++---------- client/js/models.js | 51 ++++++++++++++++++++++++++++++++++++++++++++- lib/server.js | 43 ++++++++++++++++++++++++++++++++------ 5 files changed, 123 insertions(+), 21 deletions(-) diff --git a/app.js b/app.js index 51cbadf9..a5993a67 100644 --- a/app.js +++ b/app.js @@ -1,6 +1,6 @@ var argv = require("commander") .option("-p, --port ", "port to use", parseInt) - .parse(process.argv); + .parse(process.argv); PORT = 80; // Default port. if (argv.port) { diff --git a/client/index.html b/client/index.html index f1a7bc58..97c0de4a 100644 --- a/client/index.html +++ b/client/index.html @@ -12,8 +12,8 @@ diff --git a/client/js/client.js b/client/js/client.js index ac802904..f0cb8cfd 100644 --- a/client/js/client.js +++ b/client/js/client.js @@ -1,5 +1,5 @@ /** - * The Client class + * The Client class. * * @public */ @@ -7,32 +7,54 @@ function Client() { /** - * Self references. + * Self reference. * * @private */ var self = this; + + /** + * List of networks. + * + * @type {Array} + * @public + */ + + this.networks = []; /** * The active socket. * - * @private + * @type {Socket} + * @public */ - - var socket = io.connect("") - .on("init", function(data) { self.init(data); }); + + this.socket; /** - * Set up new socket connections. + * Connect to the server via WebSockets and start listening + * to events sent by the server. * - * @param {String} data + * @param {String} host * @public */ - this.init = function(data) { - // Debug - console.log(data); + this.connect = function(host) { + this.socket = io.connect(host) + .on("init", function(networks) { self.networks = networks; }) + .on("event", this.handleEvent); }; + /** + * Handle events sent by the server. + * + * @param {Event} event + * @public + */ + + this.handleEvent = function(event) { + // Debug + console.log(event); + }; }; diff --git a/client/js/models.js b/client/js/models.js index eb7fc16b..b3f2ea88 100644 --- a/client/js/models.js +++ b/client/js/models.js @@ -1,7 +1,7 @@ (function(exports) { /** - * Declare the namespaces. + * Declare the namespace. * * @namespace */ @@ -139,4 +139,53 @@ this.user = ""; }; + /** + * Event model. + * + * Used when pushing changes between the server + * and the clients. + * + * @public + */ + + models.Event = function() { + + /** + * Action to perform. + * + * @type {String} + * @public + */ + + this.action = ""; + + /** + * Model type. + * + * @type {String} + * @public + */ + + this.type = ""; + + /** + * The target network or channel. + * + * @type {String} + * @public + */ + + this.target = undefined; + + /** + * The data. + * + * @type {Int|String|Object} + * @public + */ + + this.data = ""; + + }; + })(this); diff --git a/lib/server.js b/lib/server.js index e39c6f20..d7dd441f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -23,15 +23,34 @@ module.exports = Server; function Server() { /** - * Active sockets. + * Self reference. * * @private */ - var sockets; - + var self = this; + /** - * Start the server and listen to the specified port. + * List of networks. + * + * @type {Array} + * @public + */ + + this.networks = []; + + /** + * Active sockets managed by socket.io. + * + * @type {Object} + * @public + */ + + this.sockets; + + /** + * Start the server and listen for connections + * on the specified port. * * @param {Int} port * @public @@ -41,7 +60,7 @@ function Server() { var app = connect().use(connect.static("client")) .listen(port); - var sockets = + this.sockets = io.listen(app).on("connection", this.init) .sockets; }; @@ -54,7 +73,19 @@ function Server() { */ this.init = function(socket) { - // .. + self.sockets.emit("init", self.networks); + socket.on("input", self.handleUserInput); }; + /** + * Handle incoming inputs sent from clients. + * + * @param {String} input + * @public + */ + + this.handleUserInput = function(input) { + // Debug + console.log(input); + }; };