-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.js
105 lines (68 loc) · 2.18 KB
/
middleware.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
// middleware.js
(function() {
var liveObj = {
"users" : [],
"controller" : {"key1": 1, "key2": 0, "key3": 1}
}
var connectedIds = [];
function getLiveObj() {
/*console.log(liveObj);*/
return liveObj;
}
function addClient(clientId, type, extras) {
//default func params
type = type || "unknown-device";
extras = extras || false;
if(type === 'stage' && stageIsConnected()){
console.log('!!! No more than 1 stages are allowed');
showClientsList();
return;
}
if(connectedIds.pushUnique(clientId)){
liveObj.users.push( {"id" : clientId, "user_id" : extras.user_id, "type" : type, "extras" : extras} );
showClientsList();
}
}
function removeClient(clientId) {
for (var i = 0, len = liveObj.users.length; i < len; i++) {
if(liveObj.users[i].id === clientId){
liveObj.users.splice(i, 1);
showClientsList();
break;// breaks loop after delete
};
}
}
function showClientsList(){
console.log("- - - - - - - - - - - -");
console.log("Total users connected: ", liveObj.users.length);
for (var i = 0, len = liveObj.users.length; i < len; i++) {
console.log(i + ') ' + liveObj.users[i].type + ": ", liveObj.users[i].user_id + ' (' + liveObj.users[i].id + ')');
}
console.log("- - - - - - - - - - - -");
}
function stageIsConnected(){
for (var i = 0, len = liveObj.users.length; i < len; i++) {
if(liveObj.users[i].type === 'stage'){
return true;
}
}
}
function modifyAudioObject(data){
console.log('-----audio----', data);
liveObj.audio_stream.push(data);
return liveObj;
}
module.exports.getLiveObj = getLiveObj;
module.exports.addClient = addClient;
module.exports.removeClient = removeClient;
module.exports.stageIsConnected = stageIsConnected;
module.exports.modifyAudioObject = modifyAudioObject;
})();
Array.prototype.pushUnique = function (item){
if(this.indexOf(item) == -1) {
//if(jQuery.inArray(item, this) == -1) {
this.push(item);
return true;
}
return false;
}