-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
181 lines (141 loc) · 7.06 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
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
//List of all users currently sharing their location (tracked users)
var trackedUsers = {}
//List of all users currently tracking someones location (tracking users)
var trackedUsersTrackers = {}
app.get('/', function(req, res){
res.send('<h1>Real time tracker</h1>');
});
http.listen(3000, function(){
console.log('Listening on *:3000');
});
//-----------------------------------------------------------------------------------------------------//
io.on('connection', function(clientSocket){
console.log('a user connected');
//Messages received ---------------------------------------------------------------------------------//
//When a user is disconnected from the server
clientSocket.on('disconnect', function(){
console.log('user disconnected');
//Remove user from tracked user list
disconnectTrackedUser(clientSocket.id);
});
//A user has started sharing location
clientSocket.on("connectTrackedUser", function(nickname) {
//Add user on the tracked user list
connectTrackedUser(clientSocket.id, nickname)
});
//A user has stopped sharing location
clientSocket.on("disconnectTrackedUser", function() {
//Remove user from the tracked user list and emmit a message to the clients
disconnectTrackedUser(clientSocket.id);
});
//When the app requests a updated list of tracked users (on app start for example)
clientSocket.on("requestUpdatedTrackedUsersList", function() {
emitTrackedUsersListUpdate();
});
//A user has started tracking a user who is sharing location
clientSocket.on("connectTrackedUserTracker", function(trackedUserSocketId) {
connectTrackedUserTracker(trackedUserSocketId, clientSocket)
});
//A user has stopped tracking a user who is sharing location
clientSocket.on("disconnectTrackedUserTracker", function(trackedUserSocketId) {
disconnectTrackedUserTracker(trackedUserSocketId, clientSocket.id)
});
//Gets the coordinates of the tracked user and send it to her/his tracking users
clientSocket.on("trackedUserCoordinates", function(latitude, longitude) {
emitCoordinatesToTrackingUsers(clientSocket.id, latitude, longitude)
});
//Helpers---------------------------------------------------------------------------------//
//Function add a tracked user in tracked users list
function connectTrackedUser(clientSocketId, nickname) {
var message = "User " + nickname + " has started tracking. ";
console.log(message);
var trackedUserInfo = {};
//The user socket id and nickname is stored and added into tracked users list
trackedUserInfo["id"] = clientSocketId;
trackedUserInfo["nickname"] = nickname;
trackedUsers[clientSocket.id] = trackedUserInfo;
//Let the app knows that tracked users list was updated
emitTrackedUsersListUpdate();
}
//Function remove a tracked user from tracked users list
function disconnectTrackedUser(clientSocketId) {
//Let everyone currently monitoring user location that the location is no longer been shared
emitTrackedUserHasStoppedSharingLocation(clientSocketId)
if (trackedUsers[clientSocketId] != null) {
var message = "User " + trackedUsers[clientSocketId]["nickname"]+ " has stopped tracking. ";
console.log(message);
delete trackedUsers[clientSocketId]
delete trackedUsersTrackers[clientSocketId]
}
//Let app know that we have a new tracked user list data
emitTrackedUsersListUpdate();
}
//Function add a tracking user in trackedUsersTrackers list
function connectTrackedUserTracker(trackedUserSocketId, clientSocket) {
if (trackedUsers[trackedUserSocketId] != null) {
var message = "User " + clientSocket.id + " is traking " + trackedUsers[trackedUserSocketId]["nickname"]
console.log(message);
//Add the user socket into the tracking users list of a given tracked user (trackedUserSocketId)
if (trackedUsersTrackers[trackedUserSocketId] == null) {
trackedUsersTrackers[trackedUserSocketId] = []
}
trackedUsersTrackers[trackedUserSocketId].push(clientSocket);
}
}
//Function remove a tracking user from trackedUsersTrackers list
function disconnectTrackedUserTracker(trackedUserSocketId, clientSocketId) {
if (trackedUsers[trackedUserSocketId] != null) {
var message = "User " + clientSocketId + " has stopped tracking " + trackedUsers[trackedUserSocketId]["nickname"]
console.log(message);
//remove the user socket of the tracking users list
for (index in trackedUsersTrackers[trackedUserSocketId]) {
if (trackedUsersTrackers[trackedUserSocketId][index].id == clientSocketId) {
trackedUsersTrackers[trackedUserSocketId].splice(index, 1);
break;
}
}
}
}
//Messages to emit ---------------------------------------------------------------------------------//
//Function send the tracked user coordinates to all her/his tracking users
function emitCoordinatesToTrackingUsers(clientSocketId, latitude, longitude) {
//Confirm if tracked user is still in the list
if (trackedUsers[clientSocketId] != null) {
var message = "Coordinates of " + trackedUsers[clientSocketId]["nickname"] + ": " + "Latitude " + latitude + " Longitude " + longitude;
console.log(message);
//Sends the coordinates for all users currently tracking the tracked user
for (index in trackedUsersTrackers[clientSocketId]) {
var socket = trackedUsersTrackers[clientSocketId][index]
//Check if client socket is still connected before send coordinates
//We can use the connected property to make some validations in order to always keep track of connected users
if (socket.connected) {
var message = "Sending to " + socket.id + socket.connected;
console.log(message);
var coordinates = {};
coordinates["latitude"] = latitude;
coordinates["longitude"] = longitude;
socket.emit("trackedUserCoordinatesUpdate", coordinates);
}
}
}
}
//Function that emmit for all users current tracking tracked user that she/he is no longger sharing location
function emitTrackedUserHasStoppedSharingLocation(clientSocket) {
for (index in trackedUsersTrackers[clientSocket]) {
var socket = trackedUsersTrackers[clientSocket][index]
if (socket.connected) {
socket.emit("trackedUserHasStoppedUpdate", trackedUsers[clientSocket]["nickname"]);
}
}
}
//Function to send the updated list of tracked users
function emitTrackedUsersListUpdate() {
var trackedUsersList = Object.keys(trackedUsers).map(function(key){
return trackedUsers[key];
});
io.emit("trackedUsersListUpdate", trackedUsersList);
}
});