-
Notifications
You must be signed in to change notification settings - Fork 0
/
server2.js
55 lines (41 loc) · 1.23 KB
/
server2.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
var cluster = require('cluster');
var util = require('util');
// Code to run if we're in the master process
if (cluster.isMaster) {
// Count the machine's CPUs
var cpuCount = require('os').cpus().length;
// Create a worker for each CPU
for (var i = 0; i < cpuCount; i += 1) {
cluster.fork();
}
// Listen for dying workers
cluster.on('exit', function (worker) {
// Replace the dead worker, we're not sentimental
console.log('Worker %d died :(', worker.id);
cluster.fork();
});
// Code to run if we're in a worker process
} else {
var port = process.env.PORT || 8001;
var express = require('express');
var app = express();
var fs = require('fs');
var options = {
key: fs.readFileSync(__dirname + '/server.key'),
cert: fs.readFileSync(__dirname + '/server.crt')
};
var server = require('http').Server(app);
var WebSocketServer = require('ws').Server,
wss = new WebSocketServer({server:server});
// for number of client
// for masked nd binary data
app.use(express.static(__dirname));
server.listen(port, function() {
console.log("Listening on : " + port );
});
wss.on('connection', function(ws) {
ws.on('message',function(message){
ws.send(message);
});
});
}