-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
43 lines (42 loc) · 1.31 KB
/
index.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
import http from "http"
import cluster from "cluster"
require("dotenv").config();
import {Socket} from "./config/socket";
import {trigger} from "./service/trigger";
let app = require("./app")
let numOfCPUs = require("os").cpus().length;
const server = http.createServer(app);
if (cluster.isMaster) {
for (let i = 0; i < numOfCPUs; i++) {
cluster.fork();
}
cluster.on("fork", function (worker) {
});
cluster.on("online", function (worker) {
});
cluster.on("listening", function (worker, addr) {
});
cluster.on("disconnect", function (worker) {
});
cluster.on("exit", (worker, code, signal) => {
cluster.fork();
});
}
if (cluster.isWorker) {
const wss = Socket.init(server);
wss.on('connection', function connection(ws) {
ws.on('message', async function incoming(message: any) {
try {
const data = JSON.parse(message);
let publisher = await trigger.initial(data['trigger'], data['data'])
ws.send(JSON.stringify(publisher))
} catch (e) {
console.log(e)
}
});
});
const {HOST, REDIS_PORT, REDIS_HOST, PORT} = process.env;
const runServer = server.listen(PORT, () => {
console.log(`Fbi-Wanted Api run ${HOST + ":" + PORT}`);
});
}