-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
51 lines (43 loc) · 1.33 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
40
41
42
43
44
45
46
47
48
49
50
51
require('colors');
var http = require("http");
// creates the server that the clients can connect to
function Server() {
var self = this;
self.activeStation;
// Listen on a web port and respond with a chunked response header.
var server = http.createServer(function(req, res){
// if no station is active
if(self.inactiveStation.call(self, req, res)) { console.error("Can't serve audio stream, no active station connected".red.bold); return; };
// writes the response headers
self.writeHeaders.call(self, req, res);
// Add the response to the clients array to receive streaming
CLIENTS.push(res);
console.log('Client connected; streaming');
});
server.listen(PORT, IP);
}
// sets the active station
Server.prototype.setActiveStation = function(station) {
this.activeStation = station;
}
// writes the headers
Server.prototype.writeHeaders = function(req, res) {
var headers = {
"Content-Type": "audio/mpeg",
'Transfer-Encoding': 'chunked'
}
res.writeHead(200, headers);
}
// tests if a station is active
// if not, respond with an 500 HTTP status
Server.prototype.inactiveStation = function(req, res) {
if(typeof this.activeStation==='undefined') {
res.writeHead(500);
res.end();
CLIENTS.push(res);
return true;
} else {
return false;
}
}
module.exports = Server;