Shuo/lib/models/client.js

42 lines
716 B
JavaScript
Raw Normal View History

2014-06-29 01:07:38 +00:00
var _ = require("lodash");
2014-06-29 19:41:02 +00:00
var Network = require("./network");
2014-06-29 01:07:38 +00:00
2014-06-26 23:05:47 +00:00
module.exports = Client;
2014-06-29 01:07:38 +00:00
var id = 0;
2014-06-26 23:05:47 +00:00
function Client(attr) {
_.merge(this, _.extend({
2014-06-29 01:07:38 +00:00
id: id++,
networks: [],
2014-06-29 19:41:02 +00:00
nick: "",
keepAlive: false,
2014-06-29 01:07:38 +00:00
sockets: null
2014-06-26 23:05:47 +00:00
}, attr));
}
2014-06-29 01:07:38 +00:00
Client.prototype.emit = function(event, data) {
2014-06-29 19:41:02 +00:00
if (this.sockets !== null) {
2014-06-29 01:07:38 +00:00
this.sockets.in(this.id).emit(event, data);
}
2014-06-29 19:41:02 +00:00
};
2014-07-02 22:00:11 +00:00
Client.prototype.find = function(id) {
var network = null;
var chan = null;
this.networks.forEach(function(n) {
chan = _.find(n.channels, {id: id});
if (chan) {
network = n;
}
});
if (network && chan) {
return {
network: network,
chan: chan
};
} else {
return false;
}
};