-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.ts
81 lines (51 loc) · 2.01 KB
/
redis.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
+----------------------------------------------------------------------+
| LiteRT Reconnect.js Library |
+----------------------------------------------------------------------+
| Copyright (c) 2007-2018 Fenying Studio |
+----------------------------------------------------------------------+
| This source file is subject to version 2.0 of the Apache license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://github.com/litert/reconnect.js/blob/master/LICENSE |
+----------------------------------------------------------------------+
| Authors: Angus Fenying <fenying@litert.org> |
+----------------------------------------------------------------------+
*/
// tslint:disable:no-console
import * as Reconnector from "..";
const connector = Reconnector.createTCPReonnector({
host: "127.0.0.1",
port: 6379,
connectTimeout: 2000
});
connector.on("connected", function() {
console.log("Connected");
const c = connector.connection;
c.on("data", function(data) {
console.log(data.toString());
}).on("end", function() {
console.log("Ended.");
});
c.write("AUTH redis-passwd\r\n");
c.write("PING\r\n");
c.write("SET a b\r\n");
c.write("MSET d 1 b 2\r\n");
c.write("GET a\r\n");
c.write("MGET d b\r\n");
setTimeout(() => connector.close(), 5000);
}).on("error", function(e) {
console.error(e);
}).on("connect_fail", function(e) {
console.error("Failed to connect.", e);
}).on("close", function() {
console.info("Closed.");
}).on("reconnecting", function(n) {
console.warn(`Reconnecting (${n + 1}th times)...`);
}).setWaitStrategy(function(n): number | false {
if (n === 5) { // abort reconnecting after 5 times.
return false;
}
return 500;
});
connector.connect();