This repository was archived by the owner on Apr 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
56 lines (46 loc) · 1.76 KB
/
index.ts
File metadata and controls
56 lines (46 loc) · 1.76 KB
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
const { io } = require('socket.io-client');
const { createServer } = require('http');
const axios = require('axios');
createServer((req: any, res: any) => res.end('HTTP Server running.')).listen(8080);
const config = require('./config');
const WSClient = io(config.auth.uri, {
auth: {
apiKey: config.auth.apiKey,
secret: config.auth.secret,
token: config.auth.token,
clientName: config.client.name,
clientID: config.client.id,
type: 'RequestClient'
}
});
console.log('[WS REQ] Trying to connect to the RequestManager WebSocket...');
WSClient.on('connect_error', (error: string) => {
console.log(
`[WS REQ] Error while trying to connect to the RequestManager Websocket! (${error})`
);
});
WSClient.on('connect', () => {
console.log(
`[WS REQ] Connected to the RequestManager WebSocket! (${WSClient.id})`
);
});
WSClient.on('disconnect', (reason: string) => {
console.log(
`[WS REQ] Disconnected from the RequestManager Websocket. (${reason})`
);
});
WSClient.on('request', async (data: any, callback: any) => {
const req = await axios(data).then((res: any) => ({ status: 'success', response: res.data })).catch((err: any) => ({ status: 'error', response: err }));
callback(Object.assign({}, data, { result: req }));
});
WSClient.on('requests', async (data: any, callback: any) => {
const result = [];
for (const requestData of data) {
const req = await axios(requestData.request).then((res: any) => ({ status: 'success', response: res.data })).catch((err: any) => ({ status: 'error', response: err }));
result.push(Object.assign({}, requestData, { result: req }));
}
callback(result);
});
WSClient.on('ping', (callback: any) => {
callback();
});