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>
// 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,7 +7,7 @@
function Client() {
/**
* Self references.
* Self reference.
*
* @private
*/
@ -15,24 +15,46 @@ function Client() {
var self = this;
/**
* The active socket.
* List of networks.
*
* @private
*/
var socket = io.connect("")
.on("init", function(data) { self.init(data); });
/**
* Set up new socket connections.
*
* @param {String} data
* @type {Array<Network>}
* @public
*/
this.init = function(data) {
// Debug
console.log(data);
this.networks = [];
/**
* 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) {
/**
* 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);
};
};