echo.io
Version:
A socket.io server implementation for laravel-echo
342 lines (280 loc) • 9.48 kB
JavaScript
;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
var Io = require('./io');
var Redis = require('./redis');
var middlewares = require('./middlewares');
var Manager = function () {
/**
* Constructor.
*
* @param options
*/
function Manager(options) {
_classCallCheck(this, Manager);
this.middlewares = new Map();
this.options = {
io: undefined,
redis: 'redis://127.0.0.1:6379'
};
this.setOptions(options || {});
}
/**
* Configure instance options.
*
* @param options
*/
_createClass(Manager, [{
key: 'setOptions',
value: function setOptions(options) {
this.options = Object.assign({}, this.options, options);
}
/**
* Get options.
*
* @return {Object}
*/
}, {
key: 'getOptions',
value: function getOptions() {
var options = arguments[0];
if ('object' === (typeof options === 'undefined' ? 'undefined' : _typeof(options))) {
this.setOptions(options);
}
return this.options;
}
/**
* Start all connections.
*
* @param srv
* @param options
*
* @return {void}
*/
}, {
key: 'listen',
value: function listen(srv, options) {
this.attach(srv, options);
}
/**
* Start all connections.
*
* @param srv
* @param options
*
* @return {void}
*/
}, {
key: 'attach',
value: function attach(srv, options) {
var _this = this;
if ((typeof srv === 'undefined' ? 'undefined' : _typeof(srv)) == 'object') {
options = srv;srv = undefined;
}
options = this.getOptions(options);
if (!this.redis) {
this.startRedisConnection(options.redis);
}
if (!this.io) {
this.startIoConnection(srv, options.io);
this.connectMiddlewares();
}
// to be replaced by an event emitter/listener
this.redis.dispatch = function (channel, message) {
_this.io.dispatch(channel, JSON.parse(message));
};
}
/**
* Start a socket.io server connection.
*
* @param srv
* @param options
*
* @return {void}
*/
}, {
key: 'startIoConnection',
value: function startIoConnection(srv, options) {
options = options || {};
options.presenceAdapter = options.presenceAdapter || this.presenceAdapter;
this.io = this.createIoServer(srv, options);
this.io.connect();
}
/**
* Start a new redis connection.
*
* @param options
*
* @return {void}
*/
}, {
key: 'startRedisConnection',
value: function startRedisConnection(options) {
if (Array.isArray(options)) {
this.redis = this.createRedisClient.apply(this, _toConsumableArray(options));
} else if (arguments.length > 1) {
this.redis = this.createRedisClient.apply(this, arguments);
} else {
this.redis = this.createRedisClient(options);
}
this.redis.connect();
}
/**
* Provide the auth secret key.
*
* @return {String}
*/
}, {
key: 'auth',
value: function auth() {
if (arguments.length) {
this.authkey = arguments[0];
} else {
return process.env.AUTH_KEY || this.authkey;
}
}
/**
* Add a new connection middleware.
*
* @param {Function} middleware
*
* @return {void}
*/
}, {
key: 'use',
value: function use(middleware) {
this.middlewares.set(middleware.name, middleware);
}
/**
* Middlewares server connection.
*
* @return {void}
*/
}, {
key: 'connectMiddlewares',
value: function connectMiddlewares() {
var _this2 = this;
this.registerDefaultsMiddlewares();
this.middlewares.forEach(function (middleware) {
return _this2.io.use(middleware.getInstance(_this2));
});
}
/**
* Register defaults middlewares.
*
* @return {void}
*/
}, {
key: 'registerDefaultsMiddlewares',
value: function registerDefaultsMiddlewares() {
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = middlewares[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var middleware = _step.value;
if (this.middlewares.has(middleware.name)) {
continue;
}
this.use(middleware);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
}
/**
* Set the presence persistance adapter.
*
* @param adapter
*/
}, {
key: 'setPresenceAdapter',
value: function setPresenceAdapter(adapter) {
this.presenceAdapter = adapter;
}
/**
* Handle new presence subscription.
*
* @param id
* @param channel
* @param socket
* @param status
*
* @return {void}
*/
}, {
key: 'subscribePresence',
value: function subscribePresence(id, channel, socket, status) {
return this.io.subscribePresence(id, channel, socket, status);
}
/**
* Handle presence unsubscribe.
*
* @param socket
* @param channel
*/
}, {
key: 'unsubscribePresence',
value: function unsubscribePresence(socket, channel) {
return this.io.unsubscribePresence(socket, channel);
}
/**
* When client event is emmitted.
*
* @param socket
* @param message
*
* @return {void}
*/
}, {
key: 'onclientmessage',
value: function onclientmessage(channel, message) {
this.io.dispatch(channel, message);
}
/**
* Create a new socket.io server.
*
* @param srv
* @param options
*
* @return {Io}
*/
}, {
key: 'createIoServer',
value: function createIoServer(srv, options) {
return new Io(srv, options);
}
/**
* Create a new ioredis client.
*
* @param port
* @param host
* @param options
*
* @return {Redis}
*/
}, {
key: 'createRedisClient',
value: function createRedisClient() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return new (Function.prototype.bind.apply(Redis, [null].concat(args)))();
}
}]);
return Manager;
}();
module.exports = Manager;