Best JavaScript code snippet using argos
ChatApp.ts
Source:ChatApp.ts
1// import * as express from "express";2import * as http from "http";3import * as socket from "socket.io";4import * as SocketRedis from "socket.io-redis";5import { promisify } from "util";6import * as Promise from "bluebird";7import * as Redis from "redis";8import ChatAppConfig from "./ChatAppConfig";9import { IApp } from "../IApp";10import { Client } from "./models/Client";11// import { Typing } from "./models/Typing";12// import { SendingMessage } from "./models/SendingMessage";13// import { Message } from "./models/Message";14import { Room } from "./models/Room";15import { json } from "body-parser";16Promise.promisifyAll(Redis.RedisClient.prototype);17Promise.promisifyAll(Redis.Multi.prototype);18// var _self: ChatApp = undefined;19export class ChatApp implements IApp {20 Express: any;21 private static Self: ChatApp;22 private RedisConnector: Redis.RedisClient;23 public Server: http.Server;24 public SocketServer;25 public SocketSession;26 Init(): void {27 this.SocketServer.on("connection", this.Connect);28 ChatApp.Self = this;29 }30 OnError(): void {31 }32 private GetRedisClient(): Redis.RedisClient {33 if(this.RedisConnector==null){34 this.RedisConnector = Redis.createClient(ChatAppConfig.RedisConfig);35 }36 return this.RedisConnector;37 }38 private Connect(socket: SocketIO.Socket): void {39 console.log(`connected pid: ${process.pid} sockerId: ${socket.id}`);40 socket.on("disconnect", ChatApp.Self.Disconnect);41 socket.on("typing", ChatApp.Self.Typing);42 socket.on("send", ChatApp.Self.Send);43 socket.on("seen", ChatApp.Self.Seen);44 socket.on("set-client", ChatApp.Self.SetClient);45 socket.on("join-room", ChatApp.Self.JoinRoom);46 socket.on("disconnect-room", ChatApp.Self.DisconnectRoom);47 socket.on("add-recipient", ChatApp.Self.AddRecipient);48 }49 private SetClient(data: any): void {50 var connectionId = this.id;51 console.log("setclient="+connectionId);52 ChatApp.Self.GetRedisClient().get(data.secureKey, function (err2, secureKeyUserId) {53 if(err2){54 return;55 }56 if(data.Id != secureKeyUserId){57 return;58 }59 //Redisten uçurabilmek için çift taraflı tut... su=socket user60 console.log("setclient="+data.Id);61 ChatApp.Self.GetRedisClient().lpush("socketuser_"+data.Id.toString(), connectionId);62 ChatApp.Self.GetRedisClient().set(connectionId, data.Id.toString());63 64 });65 }66 private Disconnect(): void {67 console.log("disconnect"+this.id);68 var connectionId = this.id;69 //Redisten uçur çıkanları70 ChatApp.Self.GetRedisClient().get(connectionId, function (err2, userId) {71 ChatApp.Self.GetRedisClient().lrem("socketuser_"+userId,0,connectionId);72 ChatApp.Self.GetRedisClient().del(connectionId); 73 console.log("user disconnected pid: ${process.pid} sockerId: "+connectionId+"+"+userId);74 });75 }76 private Typing(data: any): void {77 /*var room = new Room(data.Room.PmId);78 console.log("room key=" + room.ToKey() + "member Id=" + data.FromClient.Id + " connetion Id=" + this.id);79 ChatApp.Self.SocketServer.to(room.ToKey()).emit("typing", {80 Member: new Client(this.id, data.FromClient.Id, data.FromClient.LoginName),81 Room: {82 Key: room.ToKey(),83 Id: data.Room.PmId84 }85 });*/86 if(data.secureKey == undefined){87 return;88 }89 ChatApp.Self.GetRedisClient().get(data.secureKey, function (err2, secureKeyUserId) {90 if(err2){91 return;92 }93 if(data.FromClient.Id != secureKeyUserId){94 return;95 } 96 data.ToClients.map(x=> "socketuser_" + x ).forEach(client => {97 ChatApp.Self.GetRedisClient().lrange(client,0,-1,function (err, connections) {98 var emitData = {99 Member: data.Member,100 FromClient : data.FromClient,101 PmId:data.PmId102 };103 connections.forEach(connectionId => {104 if (connectionId != this.id) {105 ChatApp.Self.SocketServer.to(connectionId).emit("typing", emitData);106 }107 }); 108 });109 });110 });111 112 }113 private Send(data: any): void {114 if(data.secureKey == undefined){115 return;116 }117 ChatApp.Self.GetRedisClient().get(data.secureKey, function (err2, secureKeyUserId) {118 if(err2){119 return;120 }121 if(data.FromClient.Id != secureKeyUserId){122 return;123 }124 data.ToClients.map(x=> "socketuser_" + x ).forEach(client => {125 ChatApp.Self.GetRedisClient().lrange(client,0,-1,function (err, connections) {126 var emitData = {127 Member: data.Member,128 Body: data.Body,129 DateCreatedWellFormed: data.DateCreatedWellFormed,130 Html: data.Html,131 FromClient : data.FromClient,132 PmId:data.PmId133 };134 connections.forEach(connectionId => {135 if (connectionId != this.id) {136 ChatApp.Self.SocketServer.to(connectionId).emit("send", emitData);137 }138 });139 }); 140 });141 });142 }143 private Seen(data: any): void {144 if(data.secureKey == undefined){145 return;146 } 147 ChatApp.Self.GetRedisClient().get(data.secureKey, function (err2, secureKeyUserId) {148 if(err2){149 return;150 }151 if(data.FromClient.Id != secureKeyUserId){152 return;153 }154 data.ToClients.map(x=> "socketuser_" + x ).forEach(client => {155 ChatApp.Self.GetRedisClient().lrange(client,0,-1,function (err, connections) {156 var emitData = {157 FromClient : data.FromClient,158 PmId:data.PmId159 };160 connections.forEach(connectionId => {161 if (connectionId != this.id) {162 ChatApp.Self.SocketServer.to(connectionId).emit("seen", emitData);163 }164 });165 }); 166 });167 });168 }169 private AddRecipient(data: any): void {170 if(data.secureKey == undefined){171 return;172 } 173 ChatApp.Self.GetRedisClient().get(data.secureKey, function (err2, secureKeyUserId) {174 if(err2){175 return;176 }177 if(data.FromClient.Id != secureKeyUserId){178 return;179 }180 data.ToClients.map(x=> "socketuser_" + x ).forEach(client => {181 ChatApp.Self.GetRedisClient().lrange(client,0,-1,function (err, connections) {182 var emitData = {183 FromClient: data.FromClient,184 PmId: data.PmId,185 Users: data.Users186 };187 connections.forEach(connectionId => {188 if (connectionId != this.id) {189 ChatApp.Self.SocketServer.to(connectionId).emit("add-recipient", emitData);190 }191 });192 }); 193 });194 });195 }196 197 // data => pm id198 private JoinRoom(data: number): void {199 var room = new Room(data);200 console.log("join room key=" + room.ToKey() + " connetion Id=" + this.id);201 this.join(room.ToKey());202 ChatApp.Self.GetRedisClient().rpush(room.ToKey(), this.id);203 }204 // data => pm id205 private DisconnectRoom(data: number): void {206 var room = new Room(data);207 console.log("disconnect room room key=" + room.ToKey() + " connetion Id=" + this.id);208 this.leave(room.ToKey());209 ChatApp.Self.GetRedisClient().lrem(room.ToKey(), 1, this.id);210 }...
index.js
Source:index.js
...62 };63 var getCacheClient = function () {64 if (cacheClient === undefined) {65 cacheClient = cacheClientBuilder({66 client: getRedisClient(),67 prefix: config.redis.prefix68 });69 if (config.redis.purgeOnLoad) {70 getRedisClient().on('connect', function () {71 cacheClient.purge();72 });73 }74 }75 return cacheClient;76 };77 var getSessionStore = function (session) {78 if (redisStore === undefined) {79 var RedisStore = require('connect-redis')(session);80 redisStore = new RedisStore({81 client: getRedisClient(),82 prefix: config.redis.prefix + 'session:'83 });84 }85 return redisStore;86 };87 module.exports.redisClient = undefined;88 module.exports.getRedisClient = getRedisClient;89 module.exports.getSessionStore = getSessionStore;90 module.exports.getCacheClient = getCacheClient;91 module.exports.createClient = newClient;...
cache.service.ts
Source:cache.service.ts
...4@Injectable()5export class CacheService {6 public redisClient: Redis;7 constructor(private redisService: RedisService) {8 this.getRedisClient().then();9 }10 /**11 * è·åredisçclient12 */13 async getRedisClient() {14 this.redisClient = await this.redisService.getClient();15 if (this.redisClient) {16 console.info('redisè¿æ¥æå');17 }18 }19 /**20 * @Description: å°è£
设置redisç¼åçæ¹æ³21 * @param key {String} keyå¼22 * @param value {String} keyçå¼23 * @param seconds {Number} è¿ææ¶é´ ç§ç§ç§ï¼ï¼ï¼24 * @return: Promise<any>25 */26 //设置å¼çæ¹æ³27 public async set(28 key: string,29 value: string | number | Buffer,30 seconds?: number,31 ) {32 value = JSON.stringify(value);33 if (!this.redisClient) {34 await this.getRedisClient();35 }36 if (!seconds) {37 await this.redisClient.set(key, value);38 } else {39 await this.redisClient.set(key, value, 'EX', seconds);40 }41 }42 //设置å¼çæ¹æ³hash43 public async hSet(key: string, mapKey: string, value: any) {44 value = JSON.stringify(value);45 if (!this.redisClient) {46 await this.getRedisClient();47 }48 await this.redisClient.hset(key, mapKey, value);49 }50 //è·åå¼çæ¹æ³51 public async get<T>(key: string): Promise<T | undefined> {52 if (!this.redisClient) {53 await this.getRedisClient();54 }55 const data = await this.redisClient.get(key);56 if (!data) {57 return undefined;58 }59 return JSON.parse(data);60 }61 //è·åå¼çæ¹æ³62 public async hGet<T>(key: string, mapKey: string): Promise<T | undefined> {63 if (!this.redisClient) {64 await this.getRedisClient();65 }66 const data = await this.redisClient.hget(key, mapKey);67 if (!data) {68 return undefined;69 }70 return JSON.parse(data);71 }72 //è·åå¼çæ¹æ³73 public async del(key: string) {74 if (!this.redisClient) {75 await this.getRedisClient();76 }77 await this.redisClient.del(key);78 }79 //å é¤hashçkey80 public async hDel(key: string, mapKey: string) {81 if (!this.redisClient) {82 await this.getRedisClient();83 }84 await this.redisClient.hdel(key, mapKey);85 }86 // æ¸
çç¼å87 public async flushall(): Promise<any> {88 if (!this.redisClient) {89 await this.getRedisClient();90 }91 await this.redisClient.flushall();92 }...
Using AI Code Generation
1const argosy = require('argosy')2const argosyRedis = require('argosy-redis')3const redis = argosyRedis({4})5const service = argosy()6service.pipe(redis()).pipe(service)7service.on('error', function (err) {8 console.error(err)9})10service.getRedisClient().then(function (client) {11 client.set('foo', 'bar')12 client.get('foo', function (err, value) {13 console.log(value)14 })15})
Using AI Code Generation
1var argosyRedis = require('argosy-redis')2var argosy = require('argosy')3var redis = require('redis')4var redisClient = redis.createClient()5var argosyRedisClient = argosyRedis({6 getRedisClient: function (callback) {7 callback(null, redisClient)8 }9})10var argosyService = argosy()11argosyService.use(argosyRedisClient)12argosyService.act('role:redis,cmd:set,key:foo,value:bar', function (err, result) {13 console.log('result', result)14 argosyService.act('role:redis,cmd:get,key:foo', function (err, result) {15 console.log('result', result)16 argosyService.close()17 redisClient.quit()18 })19})20var argosyRedis = require('argosy-redis')21var argosy = require('argosy')22var redis = require('redis')23var redisClient = redis.createClient()24var argosyRedisClient = argosyRedis({25})26var argosyService = argosy()27argosyService.use(argosyRedisClient)28argosyService.act('role:redis,cmd:set,key:foo,value:bar', function (err, result) {29 console.log('result', result)30 argosyService.act('role:redis,cmd:get,key:foo', function (err, result) {31 console.log('result', result)32 argosyService.close()33 redisClient.quit()34 })35})36### `argosyRedis(options)`37A [redis connection string](
Using AI Code Generation
1var argosyRedis = require('argosy-redis')2var redis = require('redis')3var client = redis.createClient()4var getRedisClient = argosyRedis(client).getRedisClient5getRedisClient(function (err, client) {6 console.log('getRedisClient', err, client)7})
Using AI Code Generation
1const argosRedis = require('argos-redis');2const redisClient = argosRedis.getRedisClient();3const argosRedis = require('argos-redis');4const argosRedis = require('argos-redis');5const redisClient = argosRedis.getRedisClient();6const argosRedis = require('argos-redis');7const argosRedis = require('argos-redis');8const redisClient = argosRedis.getRedisClient();9const argosRedis = require('argos-redis');10const argosRedis = require('argos-redis');11const redisClient = argosRedis.getRedisClient();12const argosRedis = require('argos-redis');13const argosRedis = require('argos-redis');14const redisClient = argosRedis.getRedisClient();15const argosRedis = require('argos-redis');16const argosRedis = require('argos-redis');17const redisClient = argosRedis.getRedisClient();18const argosRedis = require('argos-redis');
Using AI Code Generation
1const argosyRedis = require('argosy-redis-transport')2const redis = argosyRedis.getRedisClient()3redis.set('key', 'value', (err, res) => {4 if (err) {5 console.log('Error while setting key value pair in redis')6 }7 console.log('Successfully set key value pair in redis')8})
Using AI Code Generation
1var argosyRedisAdapter = require('argosy-redis-adapter');2var client = argosyRedisAdapter.getRedisClient();3client.set('foo', 'bar', function(err, reply) {4 console.log(reply);5});6### `getRedisClient([options])`
Using AI Code Generation
1var argos = require('argos-sdk');2var client = argos.getRedisClient();3client.set('test', 'test', function (err, reply) {4 console.log('err: ', err);5 console.log('reply: ', reply);6});
Using AI Code Generation
1var argosRedis = require('argos-redis');2var redisClient = argosRedis.getRedisClient();3redisClient.then(function(redisClient){4});5var argosRedis = require('argos-redis');6var redisClient = argosRedis.getRedisClient();7redisClient.then(function(redisClient){8});9var argosRedis = require('argos-redis');10var redisClient = argosRedis.getRedisClient();11redisClient.then(function(redisClient){12});13var argosRedis = require('argos-redis');14var redisClient = argosRedis.getRedisClient();15redisClient.then(function(redisClient){16});17var argosRedis = require('argos-redis');18var redisClient = argosRedis.getRedisClient();19redisClient.then(function(redisClient){20});21var argosRedis = require('argos-redis');22var redisClient = argosRedis.getRedisClient();23redisClient.then(function(redisClient){24});25var argosRedis = require('argos-redis');26var redisClient = argosRedis.getRedisClient();27redisClient.then(function(redisClient){28});29var argosRedis = require('argos-redis
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!