connections.js
3.52 KB
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
var Immutable = require("immutable");
/**
* Track connected clients
* @param {UI} ui
* @param {BrowserSync} bs
*/
module.exports.init = function (ui, bs) {
var uaParser = new bs.utils.UAParser();
var currentConnections = [];
ui.clients.on("connection", function (client) {
client.on("client:heartbeat", function (data) {
var match;
if (currentConnections.some(function (item, index) {
if (item.id === client.id) {
match = index;
return true;
}
return false;
})) {
if (typeof match === "number") {
currentConnections[match].timestamp = new Date().getTime();
currentConnections[match].data = data;
}
} else {
currentConnections.push({
id: client.id,
timestamp: new Date().getTime(),
browser: uaParser.setUA(client.handshake.headers["user-agent"]).getBrowser(),
data: data
});
}
});
});
var registry;
var temp;
var initialSent;
var int = setInterval(function () {
var sockets = ui.clients.sockets;
var keys = Object.keys(sockets);
if (keys.length) {
temp = Immutable.List(keys.map(function (clientKey) {
var currentClient = sockets[clientKey];
return Immutable.fromJS({
id: currentClient.id,
browser: uaParser.setUA(currentClient.handshake.headers["user-agent"]).getBrowser()
});
}));
if (!registry) {
registry = temp;
sendUpdated(ui.socket, decorateClients(registry.toJS(), currentConnections));
} else {
if (Immutable.is(registry, temp)) {
if (!initialSent) {
sendUpdated(ui.socket, decorateClients(registry.toJS(), currentConnections));
initialSent = true;
}
} else {
registry = temp;
sendUpdated(ui.socket, decorateClients(registry.toJS(), currentConnections));
}
}
} else {
sendUpdated(ui.socket, []);
}
}, 1000);
bs.registerCleanupTask(function () {
clearInterval(int);
});
};
/**
* Use heart-beated data to decorate clients
* @param clients
* @param clientsInfo
* @returns {*}
*/
function decorateClients(clients, clientsInfo) {
return clients.map(function (item) {
clientsInfo.forEach(function (client) {
if (client.id === item.id) {
item.data = client.data;
return false;
}
});
return item;
});
}
/**
* @param socket
* @param connectedClients
*/
function sendUpdated(socket, connectedClients) {
socket.emit("ui:connections:update", connectedClients);
}
/**
* @param clients
* @param data
*/
//function highlightClient (clients, data) {
// var socket = getClientById(clients, data.id);
// if (socket) {
// socket.emit("highlight");
// }
//}
/**
* @param clients
* @param id
*/
//function getClientById (clients, id) {
// var match;
// clients.sockets.some(function (item, i) {
// if (item.id === id) {
// match = clients.sockets[i];
// return true;
// }
// });
// return match;
//}