-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
59 lines (46 loc) · 1.54 KB
/
worker.js
File metadata and controls
59 lines (46 loc) · 1.54 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
57
58
59
var SCWorker = require('socketcluster/scworker');
var express = require('express');
var serveStatic = require('serve-static');
var path = require('path');
var morgan = require('morgan');
var healthChecker = require('sc-framework-health-check');
class Worker extends SCWorker {
run() {
console.log(' >> Worker PID:', process.pid);
var environment = this.options.environment;
var app = express();
var httpServer = this.httpServer;
var scServer = this.scServer;
if (environment === 'dev') {
// Log every HTTP request. See https://github.com/expressjs/morgan for other
// available formats.
app.use(morgan('dev'));
}
app.use(serveStatic(path.resolve(__dirname, 'public')));
// Add GET /health-check express route
healthChecker.attach(this, app);
httpServer.on('request', app);
var count = 0;
/*
In here we handle our incoming realtime connections and listen for events.
*/
scServer.on('connection', function (socket) {
// Some sample logic to show how to handle client events,
// replace this with your own logic
socket.on('sampleClientEvent', function (data) {
count++;
console.log('Handled sampleClientEvent', data);
scServer.exchange.publish('sample', count);
});
var interval = setInterval(function () {
socket.emit('random', {
number: Math.floor(Math.random() * 5)
});
}, 1000);
socket.on('disconnect', function () {
clearInterval(interval);
});
});
}
}
new Worker();