-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLocationSharing.js
205 lines (188 loc) · 8.13 KB
/
LocationSharing.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const socket = require('socket.io');
let Driver = require('./Model/Driver');
const auth = require('./utils/jwtauth');
let activeDrivers = [];
let userList = [];
let tokenID = 0;
const queueWaitingTime = 20*60; // In Seconds
const tokenIDResetTime = 2*24*60*60; // In Seconds
const driverWaitingTime = 20*60 // In Seconds
let io;
module.exports.removeDriver = (phoneNumber) => {
const driverID = activeDrivers.findIndex(driver => driver.phoneNumber === phoneNumber);
if(driverID !== -1){
activeDrivers.splice(driverID, 1);
io.emit('removeDriver', {phoneNumber});
}
}
module.exports.setupSocket = (server) => {
io = socket(server, {cookie: false});
// Automatically remove users from the queue after they have waited for more than queueWaitingTime Seconds
// Change the interval time to an appropriate value
// With the current interval, in worst case, user will stay in queue for 2*queueWaitingTime seconds
setInterval(() => {
let numOfUsers = 0;
while(numOfUsers < userList.length &&
Date.now() - userList[numOfUsers].timeStamp > queueWaitingTime*1000){
// Send remove user broadcast for these users
io.emit('removeUser', {id: userList[numOfUsers].id})
numOfUsers++;
}
userList.splice(0, numOfUsers);
console.log(`removed ${numOfUsers} users from the queue.`);
}, queueWaitingTime*1000);
// Reset the tokenID to 0 after tokenIDResetTime
setInterval(() => {tokenID = 0;}, tokenIDResetTime*1000);
// Remove inactive drivers periodically
setInterval(() => {
const inactiveDriversInd= [];
const activeDriversInd= [];
activeDrivers.forEach((driver, index) => {
if(Date.now()-driver.timeStamp > driverWaitingTime*1000){
inactiveDriversInd.push(index);
io.emit('removeDriver', {phoneNumber: driver.phoneNumber});
} else{
activeDriversInd.push(index);
}
});
activeDrivers = activeDriversInd.map(index => activeDrivers[index]);
console.log(`Removed ${inactiveDriversInd.length} drivers from the list.`)
}, driverWaitingTime*1000);
/** =======================================
* Responses to the User socket Calls
** ========================================*/
io.on('connection', (socket) => {
let userDriverList = {userList:userList, driverList:activeDrivers};
console.log(`made socket connection: ${socket.id}`);
// Provide base-data (requested while establishing new connetion or while reconnecting)
socket.on('onConnection', () => {
socket.emit('connectionResponse', userDriverList);
});
socket.on('book', (user) => {
user.id = tokenID++;
console.log(`BOOK: ${user.id}`);
userList.push(user);
// Generate token (conains userID and timeStamp) and add it to bookRespose
const token = auth.signInfo({id: user.id, timeStamp: Date.now()}, queueWaitingTime);
// Confirm Booking
socket.emit('bookResponse', {...user, token});
// Notify to all (don't emit token to all)
socket.broadcast.emit('addUser', user);
});
socket.on('unbook', (userToken) => {
// Validate Token
const userInfo = auth.verifyInfo(userToken);
const userID = userInfo.id;
if(!userInfo || userID === null){
// Invalid token
socket.emit('userAuthFailed');
return;
}
console.log(`UNBOOK: ${userID}`);
const index = userList.findIndex(user => user.id == userID);
// Remove user from the user array
if(index !== -1){
userList.splice(index, 1);
} else{
console.warn(`USER ID ${userID} is not present in users array!`);
}
// Confirm UnBook
socket.emit('unbookResponse', {id: userID});
// Notify to all
socket.broadcast.emit('removeUser', {id: userID});
});
socket.on('gotIn', (userToken) => {
// Validate Token
const userInfo = auth.verifyInfo(userToken);
const userID = userInfo.id;
if(!userInfo || userID === null){
// Invalid token
socket.emit('userAuthFailed');
return;
}
console.log(`GOT IN: ${userID}`);
const index = userList.findIndex(user => user.id == userID);
// Remove user from the user array
if(index !== -1){
userList.splice(index, 1);
} else{
console.warn(`USER ID ${userID} is not present in users array!`);
}
// Confirm GotIn
socket.emit('gotInResponse', {id: userID});
// Notify to all
socket.broadcast.emit('removeUser', {id: userID});
});
/** =======================================
* Responses to the Driver socket Calls
** ========================================*/
socket.on('registerDriver', (driver) => {
// Verify JWT token
const driverInfo = auth.verifyInfo(driver.token);
if(!driverInfo){
// If unsuccessful send Auth failed
socket.emit('driverAuthFailed', {message: 'Login Again!'});
return;
}
if(activeDrivers.find(driverData => driverData.phoneNumber === driverInfo.phoneNumber)){
// Driver already exists
return;
}
// Add driver in activeDriversList
const newDriver = new Driver(driverInfo.phoneNumber, driver.location, Date.now());
activeDrivers.push(newDriver);
console.log(`REGISTER DRIVER: ${newDriver.phoneNumber}`);
// Send addDriver broadcast to all
io.emit('addDriver', newDriver);
});
socket.on('updateDriverLocation', (driverData) => {
// Verify JWT token
const driverInfo = auth.verifyInfo(driverData.token);
if(!driverInfo){
// If unsuccessful send Auth failed
socket.emit('driverAuthFailed', {message: 'Login Again!'});
return;
}
// Find driver in activeDrivers list
const driver = activeDrivers.find(tmpDriver => tmpDriver.phoneNumber === driverInfo.phoneNumber);
if(!driver){
// If unsuccessful send Auth failed
socket.emit('driverAuthFailed', {message: 'Login Again!'});
return;
}
// Update location
driver.updateLocation(driverData);
console.log(`UPDATE LOCATION: ${driver.phoneNumber}`);
// send updateLocation broadcast to all
io.emit('updateDriverLocation', {
location: driver.location,
phoneNumber:driver.phoneNumber,
timeStamp: driver.timeStamp
});
});
socket.on('updateDriverData', (driverData) => {
// Verify JWT token
const driverInfo = auth.verifyInfo(driverData.token);
if(!driverInfo){
// If unsuccessful send Auth failed
socket.emit('driverAuthFailed', {message: 'Login Again!'});
return;
}
// Find driver in activeDrivers list
const driver = activeDrivers.find(tmpDriver => tmpDriver.phoneNumber === driverInfo.phoneNumber);
if(!driver){
// If unsuccessful send Auth failed
socket.emit('driverAuthFailed', {message: 'Login Again!'});
return;
}
// Update data
driver.updateData(driverData);
// send updateLocation broadcast to all
console.log(`UPDATE DATA: ${driver.phoneNumber}`);
io.emit('updateDriverData', driver);
});
socket.on('driverLogOut', (driverData) => {
io.emit('removeDriver', {phoneNumber: driverData.phoneNumber});
});
});
}