..
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 | import ReconnectingWebSocket from 'reconnecting-websocket';
import { WebSocket } from 'ws';
import { FastifyRequest, FastifyReply, FastifyInstance, RegisterOptions } from 'fastify';
class RapidCloud {
private readonly baseUrl =
'wss://ws1.rapid-cloud.co/socket.io/?EIO=4&transport=websocket';
private socket: ReconnectingWebSocket;
public sId = undefined;
constructor() {
this.socket = new ReconnectingWebSocket(this.baseUrl, undefined, {
WebSocket: WebSocket,
});
try {
this.socket.onopen = () => {
this.socket.send('40');
};
this.socket.onmessage = ({ data }) => {
if (data?.startsWith('40')) {
this.sId = JSON.parse(data.split('40')[1]).sid;
} else if (data == '2') {
console.log("recieved pong from RapidCloud's server");
this.socket.send('3');
}
};
this.socket.onerror = (err) => {
console.error('Websocket error: ', err);
};
setInterval(() => {
this.socket.send('3');
}, 25000);
setInterval(() => {
this.socket.reconnect();
}, 7200000);
} catch (err) {
console.log(err);
}
}
returnSID = async (fastify: FastifyInstance, options: RegisterOptions) => {
fastify.get('/rapid-cloud', async (request: FastifyRequest, reply: FastifyReply) => {
reply.status(200).send(this.sId);
});
};
}
export default RapidCloud;
|
|