Added the Event model

This commit is contained in:
Mattias Erming 2014-03-04 13:50:43 -08:00
parent 156a8748ef
commit 3fe9022d3e
5 changed files with 123 additions and 21 deletions

2
app.js
View file

@ -1,6 +1,6 @@
var argv = require("commander")
.option("-p, --port <n>", "port to use", parseInt)
.parse(process.argv);
.parse(process.argv);
PORT = 80; // Default port.
if (argv.port) {

View file

@ -12,8 +12,8 @@
<script src="/js/client.js"></script>
<script>
// Run the client.
new Client();
// Run the client!
new Client().connect("");
</script>
</body>

View file

@ -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<Network>}
* @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);
};
};

View file

@ -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);

View file

@ -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<Network>}
* @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);
};
};