Shuo/lib/server.js

34 lines
590 B
JavaScript
Raw Normal View History

2014-03-04 16:31:52 +00:00
var connect = require("connect");
2014-03-07 03:18:53 +00:00
var io = require("socket.io");
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
// Local library
var models = require("./models.js");
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
module.exports = Server;
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
function Server() {
this.sockets = false;
this.networks = new models.NetworkCollection;
}
2014-03-06 15:11:25 +00:00
2014-03-07 21:24:02 +00:00
Server.prototype.listen = function(port) {
var self = this;
2014-03-06 15:11:25 +00:00
var http = connect()
.use(connect.static("client"))
.listen(port);
2014-03-07 03:18:53 +00:00
2014-03-07 21:24:02 +00:00
this.sockets = io.listen(http).sockets;
this.sockets.on("connection", function(socket) {
init.call(self, socket);
});
2014-03-06 18:02:43 +00:00
2014-03-07 21:24:02 +00:00
return this;
};
2014-03-06 18:02:43 +00:00
2014-03-07 21:24:02 +00:00
function init(socket) {
socket.emit(
"event",
this.networks
)
2014-03-06 18:02:43 +00:00
}