-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (106 loc) · 3.64 KB
/
index.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
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
var express = require('express');
var app = express();
var ht = require('http');
var http = ht.Server(app);
var io = require('socket.io')(http);
var path = require('path'), fs = require('fs');
var uuid = require('node-uuid');
var port = process.env.PORT || 3000;
app.use('/js', express.static('public/js'));
app.use('/css', express.static('public/css'));
app.use('/images', express.static('public/images'));
http.listen(port, function() {
console.log('listening on *:'+ port);
});
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/index.html');
});
app.get('/client.html', function(req, res) {
res.sendFile(__dirname + '/public/client.html');
});
app.get('/vj.html', function(req, res) {
res.sendFile(__dirname + '/public/vj.html');
});
app.get('/manifest.json', function(req, res) {
res.sendFile(__dirname + '/public/manifest.json');
});
var players = {};
var clients = {};
var serverSockets = [];
var serverSocket;
var controlSocket;
function config(id) {
this.id = id;
this.rotation = 0;
this.online = true;
this.param1 = 0;
this.param2 = 0;
this.color = [0, 0, 0];
}
io.on('connection', function (socket) {
socket.emit('info', { hello: 'world' });
socket.on('register', function (data) {
clients[socket.id] = data.id;
if(serverSocket) {
if(players[data.id]) {
players[data.id].online = true;
serverSocket.emit('success', {existing: true, config: players[data.id]});
} else {
players[data.id] = new config(data.id);
players[data.id].online = true;
serverSocket.emit('success', {existing: false, config: players[data.id]});
}
}
});
socket.on('control', function(data) {
console.log('VJ Controller logged in');
controlSocket = socket;
if(serverSocket) {
console.log("Server socket exists already, sending opts");
//TODO
}
})
socket.on('server_register', function(data) {
serverSocket = socket;
})
socket.on('update_vj', function(data) {
serverSocket.emit('update_sound_opts', { opts: data.opts });
})
socket.on('update_rotation', function (data) {
var p = players[data.id];
if(!p) {
console.log("Error, player not found: ", data.id);
return;
}
p.rotation = data.rotation;
p.color = data.color;
if(serverSocket) serverSocket.emit('update_player', {config: p});
// if(serverSockets.length > 0) {
// for(var i = 0; i < serverSockets.length; i++) {
// serverSockets[i].emit('update_player', {config: p});
// }
// }
});
socket.on('recording', function(data) {
var filename = uuid.v4();
var p = path.join('recordings', filename + ".wav");
// for(var i = 0; i < serverSockets.length; i++) {
// serverSockets[i].emit('associate_sound', { id: data.id, sound: data.recording.audio.dataURL.split(",")[1]});
// }
if(serverSocket) serverSocket.emit('associate_sound', { id: data.id, sound: data.recording.audio.dataURL.split(",")[1]});
});
socket.on('disconnect', function() {
var client = clients[socket.id];
if(client) {
players[client].online = false;
// if(serverSockets.length > 0) {
// for(var i = 0; i < serverSockets.length; i++) {
// serverSockets[i].emit('update_player', {config: players[client]});
// }
// }
if(serverSocket) serverSocket.emit('update_player', {config: players[client]});
console.log("Disconnected: ", client);
clients[socket.id] = undefined;
}
})
});