-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.js
87 lines (73 loc) · 2.9 KB
/
socket.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
module.exports = function (socket) {
var uuid = require("node-uuid");
var Payload = require("./class/Payload.js");
var Client = require("./class/Client.js");
var clientSocket = socket.of("/client");
var dashboardSocket = socket.of("/dashboard");
dashboardSocket.on("connection", function () {
//Immediately send stats to the dashboard upon request
Payload.send(Payload, dashboardSocket);
});
clientSocket.on("connection", function (client) {
//A client will emit a beacon after it has connected to the server
//The beacon's data will contain all the necessary tracking information
client.on("beacon", function (data) {
//If a URL isn't sent over the socket then disregard this connection
//Connections can be manually crafted! Data can be manipulated!
if (!data.url) {
return;
}
client.userId = uuid.v4();
client.url = data.url;
var newClient = new Client({
userId: client.userId,
url: client.url,
userAgent: client.request.headers["user-agent"],
screenWidth: data.screenWidth,
screenHeight: data.screenHeight,
ip: client.request.connection.remoteAddress
});
Payload.addConnection();
Payload.addData([{
type: "browsers",
value: newClient.getBrowserInfo().browser.name
}, {
type: "urls",
value: client.url
}, {
type: "screenResolutions",
value: newClient.getScreenResolution()
}, {
type: "os",
value: newClient.getBrowserInfo().os.name
}]);
Payload.addClient(newClient);
Payload.send(Payload, dashboardSocket);
});
client.on("disconnect", function () {
//There could be no URL associated with a client for many reasons
//Race conditions
//A client connecting and immediately disconnecting before their tracking data is sent
if (!client.url) {
return;
}
var killedTracker = Payload.allTrackers[client.url].clients[client.userId];
Payload.removeConnection();
Payload.removeData([{
type: "os",
value: killedTracker.getBrowserInfo().os.name
}, {
type: "screenResolutions",
value: killedTracker.getScreenResolution()
}, {
type: "browsers",
value: killedTracker.getBrowserInfo().browser.name
}, {
type: "urls",
value: client.url
}]);
Payload.removeClient(client);
Payload.send(Payload, dashboardSocket);
});
});
};