echo.io
Version:
A socket.io server implementation for laravel-echo
130 lines (104 loc) • 3.81 kB
JavaScript
;
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 redis = require('ioredis');
var Redis = function () {
/**
* Constructor.
*
* @param port
* @param host
* @param options
*
* @return {void}
*/
function Redis() {
_classCallCheck(this, Redis);
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
this.args = args;
this.cpattern = '*';
this.dispatch = function (channel, message) {};
}
/**
* Create a new client connection.
*
* @return {void}
*/
_createClass(Redis, [{
key: 'connect',
value: function connect() {
var _this = this;
this.createClient.apply(this, _toConsumableArray(this.args)).then(function () {
_this.psubscribe();
_this.onpmessage(function () {
for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
return _this.dispatch.apply(_this, _toConsumableArray(args.slice(1)));
});
}).catch(function () {});
}
/**
* Subscribe to channel pattern.
*
* @return {void}
*/
}, {
key: 'psubscribe',
value: function psubscribe() {
this.client.psubscribe(this.cPattern, function (error) {});
}
/**
* When a message is received from the redis server.
*
* @param {Function} callback
*
* @return {void}
*/
}, {
key: 'onpmessage',
value: function onpmessage(callback) {
this.client.on('pmessage', callback);
}
/**
* Create a new sub client connection.
*
* @param port
* @param host
* @param options
*
* @return {Promise}
*/
}, {
key: 'createClient',
value: function createClient(port, host, options) {
options = options || {};
this.client = new redis(port, host, Object.assign(options, { lazyConnect: true }));
return this.client.connect();
}
/**
* Set the channels pattern string.
*
* @param {String} pattern
*/
}, {
key: 'cPattern',
set: function set(pattern) {
this.cpattern = pattern;
}
/**
* Get the channels pattern string.
*
* @return {String}
*/
,
get: function get() {
return this.cpattern;
}
}]);
return Redis;
}();
module.exports = Redis;