hardlounge/lib/server.js

53 lines
940 B
JavaScript
Raw Normal View History

2014-03-04 11:31:52 -05:00
var connect = require("connect");
2014-03-06 22:18:53 -05:00
var io = require("socket.io");
2014-03-06 10:11:25 -05:00
2014-03-07 16:24:02 -05:00
// Local library
var models = require("./models.js");
2014-03-06 10:11:25 -05:00
2014-03-07 16:24:02 -05:00
module.exports = Server;
2014-03-06 10:11:25 -05:00
2014-03-07 16:24:02 -05:00
function Server() {
this.sockets = false;
this.networks = new models.NetworkCollection;
}
2014-03-06 10:11:25 -05:00
2014-03-07 16:24:02 -05:00
Server.prototype.listen = function(port) {
var self = this;
2014-03-06 10:11:25 -05:00
var http = connect()
.use(connect.static("client"))
.listen(port);
2014-03-06 22:18:53 -05:00
2014-03-07 16:24:02 -05:00
this.sockets = io.listen(http).sockets;
this.sockets.on("connection", function(socket) {
init.call(self, socket);
2014-03-09 15:27:44 -04:00
socket.on(
"input",
function(input) {
handleUserInput.call(self, input);
}
);
2014-03-07 16:24:02 -05:00
});
2014-03-06 13:02:43 -05:00
2014-03-07 16:24:02 -05:00
return this;
};
2014-03-06 13:02:43 -05:00
2014-03-07 16:24:02 -05:00
function init(socket) {
socket.emit(
"event",
this.networks
)
2014-03-06 13:02:43 -05:00
}
2014-03-09 15:27:44 -04:00
function handleUserInput(input) {
var channel = this.networks.getChannel(input.id);
if (channel) {
channel.get("messages").push(
new models.Message({user: "user", text: input.text})
);
this.sockets.emit(
"event",
this.networks
);
}
}