Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 55 additions & 48 deletions src/Plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,73 @@ var args = require('optimist').argv;
var config = require(args.config || '../config.js');
var Session = require('./Session');

function Plugins(server) {
this._plugins = [];
this._intervals = [];
this._methodPlugins = [];
this._server = server;
class Plugins
{
constructor(server)
{
this._plugins = [];
this._intervals = [];
this._methodPlugins = [];
this._server = server;

this.intervalLoad();
this.methodLoad();
}

Plugins.prototype.intervalLoad = function() {
var plugins = config.intervalPlugins;
this.intervalLoad();
this.methodLoad();
}

for(var p in plugins) {
var file = plugins[p].plugin;
var plugin = require(file);
var interval = new plugin(this._server);
intervalLoad()
{
var plugins = config.intervalPlugins;

this._intervals[p] = setInterval(function() { interval.call(this._server); }, (plugins[p].interval * 1000));
for(var p in plugins) {
var file = plugins[p].plugin;
var plugin = require(file);
var interval = new plugin(this._server);

this._intervals[p] = setInterval(function() { interval.call(this._server); }, (plugins[p].interval * 1000));
}
}
}

Plugins.prototype.methodLoad = function() {
var plugins = config.methodPlugins;
methodLoad()
{
var plugins = config.methodPlugins;

for(var method in plugins) {
var file = plugins[method].plugin;
var plugin = require(file);
Session.validMethods.push(method);
Session.prototype[method] = plugin.call;
for(var method in plugins) {
var file = plugins[method].plugin;
var plugin = require(file);
Session.validMethods.push(method);
Session.prototype[method] = plugin.call;
}
}
}

Plugins.prototype.call = function(name, socket, command) {
var hook = config.hookPlugins[name];
call()
{
var hook = config.hookPlugins[name];

for(var k in hook.plugins) {
var p = hook.plugins[k];
for(var k in hook.plugins) {
var p = hook.plugins[k];

if(p === undefined) {
err = "Unable to load plugin " + p;
log.info(err);
console.log(err);
return;
}

if(p === undefined) {
err = "Unable to load plugin " + p;
log.info(err);
console.log(err);
return;
}

if(this._plugins[name] === undefined) {
this._plugins[name] = Array();
}

if(this._plugins[name][p] === undefined) {
this.load(name, p);
if(this._plugins[name] === undefined) {
this._plugins[name] = Array();
}

if(this._plugins[name][p] === undefined) {
this.load(name, p);
}
this._plugins[name][p].call(name, socket, command);
}
}

this._plugins[name][p].call(name, socket, command);
}
}

Plugins.prototype.load = function(name, file) {
this._plugins[name][file] = require(file);
load(name, file)
{
this._plugins[name][file] = require(file);
}
}

module.exports = Plugins;
92 changes: 49 additions & 43 deletions src/Room.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,59 @@
var sets = require('simplesets');


function Room(id, server) {
this.server = server;
this.id = id;
this._sessions = new sets.Set();
if (global.config.multiprocess.enabled) {
this.sub = server.redis.sub;
this.pub = server.redis.pub;
// subscribe from here, then have a global handler that hands off to room
this.sub.psubscribe(id + ':*');
class Room
{
constructor(id, server) {
this.server = server;
this.id = id;
this._sessions = new sets.Set();
if (global.config.multiprocess.enabled) {
this.sub = server.redis.sub;
this.pub = server.redis.pub;
// subscribe from here, then have a global handler that hands off to room
this.sub.psubscribe(id + ':*');
}
}
}

module.exports = Room;
addSession(session)
{
this._sessions.add(session);
}

removeSession(session)
{
this._sessions.remove(session);
}

Room.prototype.addSession = function(session) {
this._sessions.add(session);
};
isEmpty()
{
return this._sessions.size() === 0;
}

Room.prototype.removeSession = function(session) {
this._sessions.remove(session);
};
emitFromChannel(message)
{
this._sessions.each(function(s) {
s.send(message);
});
}

Room.prototype.isEmpty = function() {
return this._sessions.size() === 0;
emit(event, data, relay)
{
relay = relay || true;
if (!global.config.multiprocess.enabled) relay = false;
var packet = JSON.stringify({method:event, data: data}) + "\r\n";
// relay is a boolean switch to control whether the data
// should be relayed to the redis channel for this room
if (relay)
this.pub.publish(this.id + ':' + this.server.workerId, packet);

this._sessions.each(function(s) {
//Dont echo events back to originiating session
if(data.userId === s.id) {
return;
}
s.send(packet);
});
}
}

Room.prototype.emitFromChannel = function(message) {
this._sessions.each(function(s) {
s.send(message);
});
};

Room.prototype.emit = function(event, data, relay) {
relay = relay || true;
if (!global.config.multiprocess.enabled) relay = false;
var packet = JSON.stringify({method:event, data: data}) + "\r\n";
// relay is a boolean switch to control whether the data
// should be relayed to the redis channel for this room
if (relay)
this.pub.publish(this.id + ':' + this.server.workerId, packet);

this._sessions.each(function(s) {
//Dont echo events back to originiating session
if(data.userId === s.id) {
return;
}
s.send(packet);
});
};
module.exports = Room;
Loading