-
Notifications
You must be signed in to change notification settings - Fork 3
/
multi
executable file
·207 lines (193 loc) · 6.4 KB
/
multi
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
#!/usr/bin/env node
"use strict";
var app = require('express')(),
express = require('express'),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
listening = [],
fs = require('fs'),
when = require('when'),
sequence = require('sequence'),
audioFiles = {},
config = {},
htmlString;
try {
config = require('./config').config;
}
catch(err) {
console.log(err);
}
server.listen(8080);
app.use("/images", express.static(__dirname + "/images"));
app.use("/static", express.static(__dirname + "/static"));
// client route
app.get('/', function (req, res) {
var ua = req.header('user-agent');
if (/mobile/i.test(ua)) {
return res.sendfile(__dirname + '/slave_mobile.html');
}
return res.sendfile(__dirname + '/slave.html');
});
// server route
app.get('/master', function(req, res) {
res.sendfile(__dirname + '/master.html');
});
// add files and set up socket
addFiles().then(function(files){
audioFiles = files;
logListeners();
io.sockets.on('connection', function (socket) {
// master event
socket.on('masterEvent', function (data) {
// master is requesting a list of slaves
if (data.audioEvent === 'requestClientList') {
var htmlString = buildClientsList();
socket.emit('masterEvent', {
'audioEvent': 'updateSources',
'sources': htmlString
});
}
// master is requesting a re-initialization of slaves
else if (data.audioEvent === 'reload') {
addFiles().then(function(files){
audioFiles = files;
var htmlString = buildClientsList();
socket.broadcast.emit('slaveEvent', data);
socket.emit('masterEvent', {
'audioEvent': 'updateSources',
'sources': htmlString
});
});
}
// master is sending a message to be broadcast
// to slaves
else {
socket.broadcast.emit('slaveEvent', data);
}
});
// slave event
socket.on('slaveEvent', function (data){
console.log(data);
// slave has loaded audio and is ready to play
if (data.audioEvent === 'loaded') {
audioFiles[data.source] = audioFiles[data.source] + 1;
htmlString = buildClientsList();
socket.broadcast.emit('masterEvent', {
'audioEvent': 'updateSources',
'sources': htmlString
});
}
// slave has recieved a play event from master
// and is requesting a latency-adjusted play event
else if (data.audioEvent === 'requestPlay') {
// get this slave's latency
socket.get('latency', function(err, latency){
socket.emit('slaveEvent', {
'audioEvent': 'latencyAdjustedPlay',
'timeout': 1000 - latency
});
});
}
// slave has closed the window or disconnected
else if (data.audioEvent === 'removeSource') {
audioFiles[data.source] = audioFiles[data.source] - 1;
htmlString = buildClientsList();
socket.broadcast.emit('masterEvent', {
'audioEvent': 'updateSources',
'sources': htmlString
});
}
// slave is requesting a source to load
else if (data.audioEvent === 'requestSource') {
var nextFile = getNextAudioFile();
console.log(nextFile);
if (nextFile){
socket.emit('slaveEvent', {
'audioEvent': 'load',
'source': nextFile
});
}
else {
nextFile = getRandomAudioFile();
socket.emit('slaveEvent', {
'audioEvent': 'load',
'source': nextFile
});
}
}
// set latency for this connected slave
else if (data.audioEvent === 'setLatency') {
// callback to client here?
socket.set('latency', data.latency, function(){});
}
});
// this is a simple call-respond to figure out socket latency
socket.on('latencyEvent', function(data){
socket.emit('latencyEvent', data);
});
});
});
// populate the files array
function addFiles(){
var d = when.defer(),
filesMap = {},
fileString;
if (config.useRemoteFiles &&
config.remotePath &&
config.remoteFiles) {
console.log('Loading remote files from config.js');
config.remoteFiles.forEach(function(file){
fileString = config.remotePath + file;
filesMap[fileString] = 0;
});
d.resolve(filesMap);
}
else {
console.log('Attempting to load local files from ./static/audio');
fs.readdir(__dirname + '/static/audio', function(err, files){
if (!err){
files.forEach(function(file){
if (file !== '.DS_Store') {
fileString = '/static/audio/' + file;
filesMap[fileString] = 0;
}
});
d.resolve(filesMap);
}
});
}
return d.promise;
}
// get next unloaded audio file
function getNextAudioFile(){
for (var file in audioFiles){
if (audioFiles[file] === 0) {
return file;
}
}
return false;
}
function getRandomAudioFile() {
var result,
count = 0,
file;
for (file in audioFiles)
if (Math.random() < 1/++count)
result = file;
return result;
}
function buildClientsList(){
var htmlString = '',
file;
for (file in audioFiles) {
if (audioFiles[file]) {
htmlString = htmlString + '<br/>' + file + ' (' + audioFiles[file] + ')';
}
}
return htmlString;
}
function logListeners(){
for (var file in audioFiles){
console.log(file + ': ' + audioFiles[file] + ' listeners.');
}
}