-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
208 lines (181 loc) · 6.29 KB
/
worker.js
File metadata and controls
208 lines (181 loc) · 6.29 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
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;
/**
* Log debug messages.
* Scoped to run function since we need environment
* @param {*} obj Message or object to be logged.
*/
function log(obj) {
if (environment === 'dev') {
console.log(obj);
}
}
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;
scServer.addMiddleware(scServer.MIDDLEWARE_SUBSCRIBE, (req, next) => {
// Allow subscribe to anything during development
// TODO: Only allow subscription to own private channel
if (true || req.socket.id === req.channel) {
next(); // Allow
} else {
let msg = `${req.socket.id} is not allowed to subscribe to ${req.channel}`;
log(msg);
next(msg); // Block
}
});
scServer.addMiddleware(scServer.MIDDLEWARE_PUBLISH_IN, (req, next) => {
// Disallow direct broadcasting right now
if (true) {
next(); // Allow
} else {
let msg = `${req.socket.id} is not allowed to publish to ${req.channel}`;
log(msg);
next(msg); // Block
}
});
scServer.addMiddleware(scServer.MIDDLEWARE_PUBLISH_OUT, (req, next) => {
// console.log(req);
next();
});
/**
* Handle incoming connections and listen for events.
*
* Every published message should include the following:
* {
* action: string,
* channel: string,
* sender: string
* }
*/
scServer.on('connection', (socket) => {
log(`${socket.id} connected.`);
/**
* Incoming format:
* {
* channel: string
* }
*/
socket.on('join', (obj) => {
if (!obj || !obj.channel || obj.channel.length === 0) {
log('REJECTED: Bad join request.');
log(obj);
return;
}
log(`Client ID ${socket.id} joined channel ${obj.channel}.`);
socket.emit('joined', { channel: obj.channel });
// Announce the join to everyone
scServer.exchange.publish(obj.channel, {
action: 'join',
channel: obj.channel,
sender: socket.id
});
});
/**
* Incoming format:
* {
* channel: string,
* recipient: string,
* offer: object
* }
*/
socket.on('offer', (obj) => {
if (!obj || !obj.channel || !obj.recipient || !obj.offer) {
log('REJECTED: Bad offer message.');
log(obj);
return;
}
scServer.exchange.publish(obj.recipient, {
action: 'offer',
channel: obj.channel,
sender: socket.id,
offer: obj.offer
});
});
/**
* Incoming format:
* {
* channel: string,
* recipient: string,
* answer: object
* }
*/
socket.on('answer', (obj) => {
if (!obj || !obj.channel || !obj.recipient || !obj.answer) {
log('REJECTED: Bad answer message.');
log(obj);
return;
}
scServer.exchange.publish(obj.recipient, {
action: 'answer',
channel: obj.channel,
sender: socket.id,
answer: obj.answer
});
});
/**
* Incoming format:
* {
* channel: string,
* recipient: string,
* candidate: object
* }
*/
socket.on('candidate', (obj) => {
if (!obj || !obj.channel || !obj.recipient || !obj.candidate) {
log('REJECTED: Bad candidate message.');
log(obj);
return;
}
scServer.exchange.publish(obj.recipient, {
action: 'candidate',
channel: obj.channel,
sender: socket.id,
candidate: obj.candidate
});
});
/**
* Incoming format:
* {
* channel: string
* }
*/
socket.on('leave', (obj) => {
if (!obj || !obj.channel || obj.channel.length === 0) {
log('REJECTED: Bad leave request.');
log(obj);
return;
}
log(`${socket.id} left channel ${obj.channel}`);
// Announce the join to everyone
scServer.exchange.publish(obj.channel, {
action: 'leave',
channel: obj.channel,
sender: socket.id
});
});
socket.on('disconnect', () => {
log(`${socket.id} disconnected.`);
});
});
}
}
new Worker();