-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (32 loc) · 1.09 KB
/
server.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
require('dotenv').config();
const net = require('net');
const Device = require('./device');
const connectRabbitMQ = require('./connectors/rabbitMQ');
const connectRedis = require('./connectors/redis');
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Create a worker for each CPU core
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
} else {
//Create an instance of the server
const server = net.createServer(onClientConnection);
//define host and port to run the server
const port = 8080;
const host = '127.0.0.1';
//Start listening with the server on given port and host.
server.listen(port, host, function () {
console.log(`Server started on port ${port} at ${host}`);
});
//Connecting RabbitMQ publisher/consumer
connectRabbitMQ();
connectRedis();
global.devices = {};
//Declare connection listener function
function onClientConnection(sock) {
console.log(`${sock.remoteAddress}:${sock.remotePort} Connected`);
new Device(sock)
};
}