-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprotocol.mjs
44 lines (44 loc) · 1.32 KB
/
protocol.mjs
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
import { Agent } from 'node:https';
import { request } from 'node:https';
async function saveState(blockchainstatus){
const path = '/protocol.php';
const clientoptions = {
hostname: 'example.com',
port: 443,
path: path,
method: 'POST',
};
let data={};
data.token='yoursecrettoken';
data.blockchainstate=blockchainstatus;
clientoptions.agent = new Agent(clientoptions);
return new Promise((resolve, reject) => {
const serverrequest = request(clientoptions, (serverresponse) => {
let responsedata = '';
serverresponse.on('data', (chunk) => {
responsedata += chunk;
});
serverresponse.on('end', () => {
try{
const responseobject = JSON.parse(responsedata.toString());
resolve(responseobject);
}catch{
console.log(responsedata.toString());
resolve({"error":"servererror"});
}
});
serverrequest.on('error', (e) => {
console.log("Requesterror:\n"+e);
reject(e);
});
serverrequest.on('timeout', () => {
nodereq.destroy();
console.log("RPC Error:\n"+e);
reject(new Error('Connection timeout'));
});
});
serverrequest.write(JSON.stringify(data));
serverrequest.end();
});
}
export { saveState };