Shuo/lib/server.js

92 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-03-04 16:31:52 +00:00
/**
* Module dependencies.
*/
var connect = require("connect");
2014-03-04 17:22:06 +00:00
var io = require("socket.io");
2014-03-04 16:31:52 +00:00
2014-03-04 19:40:27 +00:00
// Local library.
var models = require("../client/js/models.js");
2014-03-04 16:31:52 +00:00
/**
* Export module.
*/
module.exports = Server;
/**
* The Server class.
*
* @public
*/
function Server() {
2014-03-04 17:22:06 +00:00
/**
2014-03-04 21:50:43 +00:00
* Self reference.
2014-03-04 17:22:06 +00:00
*
* @private
*/
2014-03-04 21:50:43 +00:00
var self = this;
/**
* List of networks.
*
* @type {Array<Network>}
* @public
*/
this.networks = [];
/**
* Active sockets managed by socket.io.
*
* @type {Object}
* @public
*/
2014-03-04 16:31:52 +00:00
2014-03-04 21:50:43 +00:00
this.sockets;
2014-03-04 16:31:52 +00:00
/**
2014-03-04 21:50:43 +00:00
* Start the server and listen for connections
* on the specified port.
2014-03-04 16:31:52 +00:00
*
2014-03-04 19:40:27 +00:00
* @param {Int} port
2014-03-04 16:31:52 +00:00
* @public
*/
this.listen = function(port) {
2014-03-04 17:22:06 +00:00
var app = connect().use(connect.static("client"))
.listen(port);
2014-03-04 21:50:43 +00:00
this.sockets =
2014-03-04 17:22:06 +00:00
io.listen(app).on("connection", this.init)
.sockets;
2014-03-04 16:31:52 +00:00
};
2014-03-04 17:22:06 +00:00
/**
* Initiate new socket connections.
*
* @param {Socket} socket
* @public
*/
this.init = function(socket) {
2014-03-04 21:50:43 +00:00
self.sockets.emit("init", self.networks);
socket.on("input", self.handleUserInput);
2014-03-04 17:22:06 +00:00
};
2014-03-04 21:50:43 +00:00
/**
* Handle incoming inputs sent from clients.
*
* @param {String} input
* @public
*/
this.handleUserInput = function(input) {
// Debug
console.log(input);
};
2014-03-04 16:31:52 +00:00
};