This repository has been archived by the owner on Jun 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.js
81 lines (67 loc) · 1.89 KB
/
client.js
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
var net = require('net');
var client = new net.Socket();
var operation;
var ip;
var port;
var password;
module.exports = class {
getData(key, collection) {
getDataFunc(key, collection);
}
setData(name, variable) {
setDataFunc(name, variable);
}
deleteData(key) {
deleteDataFunc(key);
}
async connect(ip, port, password) {
await setIpadPort(ip, port, password);
}
}
function setIpadPort(newip, newport, newpassword) {
ip = newip;
port = newport;
password = newpassword;
}
async function deleteDataFunc(key) {
if(ip == undefined) return console.log('no ip');
if(port == undefined) return console.log('no port');
operation = 'delete';
client.connect(port, ip);
//on connect event
client.on('connect', async () => {
if(operation == 'delete') {
await client.write('delete ' + password + ' ' + key);
client.end();
}
});
}
async function getDataFunc(key, collection) {
if(ip == undefined) return console.log('no ip');
if(port == undefined) return console.log('no port');
operation = 'get';
client.connect(port, ip);
//on connect event
client.on('connect', async () => {
if(operation == 'get') {
await client.write('get ' + password + ' ' + key);
await client.on('data', function(data) {
console.log(data.toString());
client.end();
});
}
});
}
async function setDataFunc(name, variable) {
if(ip == undefined) return console.log('no ip');
if(port == undefined) return console.log('no port');
operation = 'set';
client.connect(port, ip);
//on connect event
client.on('connect', async () => {
if(operation == 'set') {
await client.write('set ' + password + ' ' + name + ' ' + variable);
client.end();
}
});
}