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

View File

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

View File

@ -1,5 +1,5 @@
/** /**
* The Client class * The Client class.
* *
* @public * @public
*/ */
@ -7,7 +7,7 @@
function Client() { function Client() {
/** /**
* Self references. * Self reference.
* *
* @private * @private
*/ */
@ -15,24 +15,46 @@ function Client() {
var self = this; var self = this;
/** /**
* The active socket. * List of networks.
* *
* @private * @type {Array<Network>}
*/
var socket = io.connect("")
.on("init", function(data) { self.init(data); });
/**
* Set up new socket connections.
*
* @param {String} data
* @public * @public
*/ */
this.init = function(data) { this.networks = [];
// Debug
console.log(data); /**
* The active socket.
*
* @type {Socket}
* @public
*/
this.socket;
/**
* Connect to the server via WebSockets and start listening
* to events sent by the server.
*
* @param {String} host
* @public
*/
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) { (function(exports) {
/** /**
* Declare the namespaces. * Declare the namespace.
* *
* @namespace * @namespace
*/ */
@ -139,4 +139,53 @@
this.user = ""; 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); })(this);

View File

@ -23,15 +23,34 @@ module.exports = Server;
function Server() { function Server() {
/** /**
* Active sockets. * Self reference.
* *
* @private * @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 * @param {Int} port
* @public * @public
@ -41,7 +60,7 @@ function Server() {
var app = connect().use(connect.static("client")) var app = connect().use(connect.static("client"))
.listen(port); .listen(port);
var sockets = this.sockets =
io.listen(app).on("connection", this.init) io.listen(app).on("connection", this.init)
.sockets; .sockets;
}; };
@ -54,7 +73,19 @@ function Server() {
*/ */
this.init = function(socket) { 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);
};
}; };