Removed all transport-specific methods from Memory store

Added publish/subscribe placeholders that basically do nothing.
This commit is contained in:
Guillermo Rauch
2011-06-27 15:40:32 -03:00
parent 5342dd6d76
commit 95e787dcc5

View File

@@ -10,7 +10,7 @@
*/
var crypto = require('crypto')
, Store = require('../store')
, Store = require('../store');
/**
* Exports the constructor.
@@ -26,9 +26,7 @@ Memory.Client = Client;
*/
function Memory (opts) {
this.handshaken = [];
this.clientsMap = {};
this.rooms = {};
Store.call(this, opts);
};
/**
@@ -38,194 +36,28 @@ function Memory (opts) {
Memory.prototype.__proto__ = Store.prototype;
/**
* Handshake a client.
*
* @param {Object} client request object
* @param {Function} callback
* @api public
*/
Memory.prototype.handshake = function (data, fn) {
var id = this.generateId();
this.handshaken.push(id);
fn(null, id);
return this;
};
/**
* Checks if a client is handshaken.
*
* @api public
*/
Memory.prototype.isHandshaken = function (id, fn) {
fn(null, ~this.handshaken.indexOf(id));
return this;
};
/**
* Generates a random id.
* Publishes a message.
*
* @api private
*/
Memory.prototype.generateId = function () {
var rand = String(Math.random() * Math.random() * Date.now());
return crypto.createHash('md5').update(rand).digest('hex');
};
Memory.prototype.publish = function () { };
/**
* Retrieves a client store instance.
*
* @api public
*/
Memory.prototype.client = function (id) {
if (!this.clientsMap[id]) {
this.clientsMap[id] = new Memory.Client(this, id);
this.log.debug('initializing client store for', id);
}
return this.clientsMap[id];
};
/**
* Called when a client disconnects.
*
* @api public
*/
Memory.prototype.disconnect = function (id, force, reason) {
if (~this.handshaken.indexOf(id)) {
this.log.debug('destroying dispatcher for', id);
this.handshaken.splice(this.handshaken.indexOf(id), 1);
if (this.clientsMap[id]) {
this.clientsMap[id].destroy();
delete this.clientsMap[id];
}
if (force)
this.publish('disconnect-force:' + id, reason);
this.publish('disconnect:' + id, reason);
}
return this;
};
/**
* Relays a heartbeat message.
* Subscribes to a channel
*
* @api private
*/
Memory.prototype.heartbeat = function (id) {
return this.publish('heartbeat-clear:' + id);
};
Memory.prototype.subscribe = function () { };
/**
* Relays a packet
* Unsubscribes
*
* @api private
*/
Memory.prototype.message = function (id, packet) {
return this.publish('message:' + id, packet);
};
/**
* Returns client ids in a particular room
*
* @api public
*/
Memory.prototype.clients = function (room, fn) {
if ('function' == typeof room) {
fn = room;
room = '';
}
fn && fn(this.rooms[room] || []);
};
/**
* Joins a user to a room
*
* @api private
*/
Memory.prototype.join = function (sid, room, fn) {
if (!this.rooms[room]) {
this.rooms[room] = [];
}
this.client(sid).rooms[room] = this.rooms[room].length;
this.rooms[room].push(sid);
fn && fn();
return this;
};
/**
* Removes a user from a room
*
* @api private
*/
Memory.prototype.leave = function (sid, room, fn) {
if (!this.rooms[room] || this.client(sid).rooms[room] === undefined) {
return this;
}
var i = this.client(sid).rooms[room];
this.rooms[room][i] = null;
delete this.client(sid).rooms[room];
fn && fn();
return this;
};
/**
* Simple publish
*
* @api public
*/
Memory.prototype.publish = function (ev, data, fn) {
if ('function' == typeof data) {
fn = data;
data = undefined;
}
this.emit(ev, data);
if (fn) fn();
return this;
};
/**
* Simple subscribe
*
* @api public
*/
Memory.prototype.subscribe = function (chn, fn) {
this.on(chn, fn);
return this;
};
/**
* Simple unsubscribe
*
* @api public
*/
Memory.prototype.unsubscribe = function (chn) {
this.removeAllListeners(chn);
};
Memory.prototype.unsubscribe = function () { };
/**
* Client constructor