/** * Subscribe to a command * * @param {String} cmd * @param {String} nodeID * * @memberof RedisTransporter */ subscribe(cmd, nodeID) { this.clientSub.subscribe(this.getTopicName(cmd, nodeID)); return this.broker.Promise.resolve(); }
/** * Send data buffer. * * @param {String} topic * @param {Buffer} data * @param {Object} meta * * @returns {Promise} */ send(topic, data) { /* istanbul ignore next*/ if (!this.clientPub) return this.broker.Promise.reject(new MoleculerError("Redis Client is not available")); this.clientPub.publish(topic, data); return this.broker.Promise.resolve(); }
/** * Disconnect from the server * * @memberof RedisTransporter */ disconnect() { if (this.clientSub) { this.clientSub.disconnect(); this.clientSub = null; } if (this.clientPub) { this.clientPub.disconnect(); this.clientPub = null; } }
transporter._clientSub.onCallbacks.connect(); // Trigger the `resolve` transporter._clientPub.onCallbacks.connect(); // Trigger the `resolve` return p; });
this._clientSub = clientSub; // For tests clientSub.on("connect", () => { this.logger.info("Redis-sub client is connected."); clientPub.on("connect", () => { this.clientSub = clientSub; this.clientPub = clientPub; clientPub.on("error", (e) => { this.logger.error("Redis-pub error", e.message); this.logger.debug(e); clientPub.on("close", () => { this.connected = false; this.logger.warn("Redis-pub client is disconnected."); clientSub.on("messageBuffer", (rawTopic, buf) => { const topic = rawTopic.toString().substring(this.prefix.length + 1); const cmd = topic.split(".")[0]; clientSub.on("error", (e) => { this.logger.error("Redis-sub error", e.message); this.logger.debug(e); clientSub.on("close", () => { this.connected = false; this.logger.warn("Redis-sub client is disconnected.");
after(() => { if (testutils.redis) { testutils.redis.quit() } // setTimeout(() => wtf.dump(), 1000) })
close () { this.closed = true return this.redis.quit().return() }
addSocket (id, userName) { return this.redis.hset( this.makeKeyName('instances', this.instanceUID, 'sockets'), id, userName) }
constructor (server, options) { this.server = server this.options = options this.closed = false if (this.options.useCluster) { this.redis = new Redis.Cluster(...this.options.redisOptions) } else { const redisOptions = _.castArray(this.options.redisOptions) this.redis = new Redis(...redisOptions) } this.RoomState = RoomStateRedis this.UserState = UserStateRedis this.DirectMessagingState = DirectMessagingStateRedis this.lockTTL = this.options.lockTTL || 10000 this.instanceUID = this.server.instanceUID this.server.redis = this.redis for (const [cmd, def] of _.toPairs(luaCommands)) { this.redis.defineCommand(cmd, { numberOfKeys: def.numberOfKeys, lua: def.lua }) } }
getInstanceSockets (uid = this.instanceUID) { return this.redis.hgetall(this.makeKeyName('instances', uid, 'sockets')) }
removeSocket (id) { return this.redis.hdel( this.makeKeyName('instances', this.instanceUID, 'sockets'), id) }