-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
50 lines (41 loc) · 1.22 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
"use strict";
var fs = require("fs");
var path = require("path");
var AirTunesServer = require("nodetunes");
var airtunes = require("airtunes");
var config = JSON.parse(fs.readFileSync("config.json"));
var server = new AirTunesServer({ serverName : config.groupName });
var endpoints = config.endpoints;
var devices;
var currentStream;
server.on("clientConnected", function(stream) {
console.log("clientConnected");
devices = [];
endpoints.forEach(function(host) {
devices.push(airtunes.add(host));
});
currentStream = stream;
stream.pipe(airtunes);
});
server.on("clientDisconnected", function() {
console.log("clientDisconnected");
currentStream.unpipe();
airtunes.stopAll(function() {
console.log("All devices stopped")
});
});
server.on("error", function(err) {
console.log("Error: " + err.message );
});
server.on("metadataChange", function(metadata) {
console.log("Now playing \"" + metadata.minm + "\" by " + metadata.asar + " from \"" + metadata.asal + "\"");
});
server.on("volumeChange", function(volume) {
volume = (volume + 30) / 30 * 100
console.log("volumeChange " + volume);
devices.forEach(function(device) {
device.setVolume(volume);
});
});
server.start();
console.log(config.groupName + " started");