Added check to ensure only the newest location is sent

master
Keith Irwin 2018-01-22 22:27:53 +00:00
parent 6aba3ec9a8
commit 3e9367e548
No known key found for this signature in database
GPG Key ID: 378933C743E2BBC0
3 changed files with 22 additions and 12 deletions

View File

@ -1,6 +1,7 @@
# Tracman Server Changelog
###### v0.8.0
* Added check to ensure only the newest location is sent
* Removed buggy login/-out redirects
* [#111](https://github.com/Tracman-org/Server/issues/111) Implemented service worker
* [#116](https://github.com/Tracman-org/Server/issues/116) Switched promises for async/await

View File

@ -56,6 +56,7 @@ Tracman will be updated according to [this branching model](http://nvie.com/post
[view full changelog](CHANGELOG.md)
#### v0.8.0
* Added check to ensure only the newest location is sent
* Removed buggy login/-out redirects
* [#111](https://github.com/Tracman-org/Server/issues/111) Implemented service worker
* [#116](https://github.com/Tracman-org/Server/issues/116) Switched promises for async/await

View File

@ -63,7 +63,7 @@ module.exports = {
debug(`Location was set to: ${JSON.stringify(loc)}`)
// Get android timestamp or use server timestamp
if (loc.ts) loc.tim = Date(loc.ts)
if (loc.ts) loc.tim = +loc.ts
else loc.tim = Date.now()
// Check for user and sk32 token
@ -93,19 +93,27 @@ module.exports = {
).message
)
} else {
// Broadcast location
io.to(loc.usr).emit('get', loc)
debug(`Broadcasting ${loc.lat}, ${loc.lon} to ${loc.usr}`)
// Save in db as last seen
user.last = {
lat: parseFloat(loc.lat),
lon: parseFloat(loc.lon),
dir: parseFloat(loc.dir || 0),
spd: parseFloat(loc.spd || 0),
time: loc.tim
// Check that loc is newer than lastLoc
debug(`Checking that loc of ${loc.tim} is newer than last of ${user.last.time.getTime()}...`)
if (!user.last.time || loc.tim > user.last.time.getTime()) {
// Broadcast location
io.to(loc.usr).emit('get', loc)
debug(`Broadcasting ${loc.lat}, ${loc.lon} to ${loc.usr}`)
// Save in db as last seen
user.last = {
lat: parseFloat(loc.lat),
lon: parseFloat(loc.lon),
dir: parseFloat(loc.dir || 0),
spd: parseFloat(loc.spd || 0),
time: loc.tim
}
user.save()
}
user.save()
}
} catch (err) { console.error(err.stack) }
}