tracman-server/config/sockets.js

124 lines
3.4 KiB
JavaScript
Raw Normal View History

'use strict';
2017-03-14 07:58:56 -06:00
// Imports
2017-05-30 12:41:23 -06:00
const debug = require('debug')('tracman-sockets'),
2017-05-06 21:25:32 -06:00
User = require('./models.js').user;
2017-03-14 07:58:56 -06:00
// Check for tracking clients
function checkForUsers(io, user) {
2017-05-08 11:47:53 -06:00
debug(`Checking for clients receiving updates for ${user}`);
2017-03-14 07:58:56 -06:00
// Checks if any sockets are getting updates for this user
2017-04-28 13:59:43 -06:00
if (Object.values(io.sockets.connected).some( (socket)=>{
return socket.gets===user;
2017-03-14 07:58:56 -06:00
})) {
2017-05-08 11:47:53 -06:00
debug(`Activating updates for ${user}.`);
2017-03-14 07:58:56 -06:00
io.to(user).emit('activate','true');
} else {
2017-05-08 11:47:53 -06:00
debug(`Deactivating updates for ${user}.`);
2017-03-14 07:58:56 -06:00
io.to(user).emit('activate', 'false');
}
}
module.exports = {
checkForUsers: checkForUsers,
init: (io)=>{
io.on('connection', (socket)=>{
2017-05-08 11:47:53 -06:00
debug(`${socket.id} connected.`);
2017-04-24 11:24:41 -06:00
// Set a few variables
//socket.ip = socket.client.request.headers['x-real-ip'];
//socket.ua = socket.client.request.headers['user-agent'];
2017-03-15 04:30:03 -06:00
2017-06-27 11:36:46 -06:00
// Log and errors
2017-05-06 21:27:49 -06:00
socket.on('log', (text)=>{
2017-05-08 11:47:53 -06:00
debug(`LOG: ${text}`);
2017-05-06 21:27:49 -06:00
});
2017-06-27 11:36:46 -06:00
socket.on('error', (err)=>{ console.error('❌', err.stack); });
2017-03-14 07:58:56 -06:00
// This socket can set location (app)
socket.on('can-set', (userId)=>{
2017-05-08 11:47:53 -06:00
debug(`${socket.id} can set updates for ${userId}.`);
socket.join(userId, ()=>{
2017-05-08 11:47:53 -06:00
debug(`${socket.id} joined ${userId}`);
2017-03-14 07:58:56 -06:00
});
checkForUsers( io, userId );
});
// This socket can receive location (map)
socket.on('can-get', (userId)=>{
2017-03-14 07:58:56 -06:00
socket.gets = userId;
2017-05-08 11:47:53 -06:00
debug(`${socket.id} can get updates for ${userId}.`);
socket.join(userId, ()=>{
2017-05-08 11:47:53 -06:00
debug(`${socket.id} joined ${userId}`);
2017-03-14 07:58:56 -06:00
socket.to(userId).emit('activate', 'true');
});
});
// Set location
socket.on('set', (loc)=>{
2017-05-08 11:47:53 -06:00
debug(`${socket.id} set location for ${loc.usr}`);
debug(`Location was set to: ${JSON.stringify(loc)}`);
2017-03-14 07:58:56 -06:00
2017-05-06 23:12:54 -06:00
// Get android timestamp or use server timestamp
if (loc.ts){ loc.tim = Date(loc.ts); }
else { loc.tim = Date.now(); }
2017-04-23 07:05:35 -06:00
// Check for user and sk32 token
if (!loc.usr){
2017-04-25 07:56:04 -06:00
console.error("❌", new Error(`Recieved an update from ${socket.ip} without a usr!`).message);
2017-04-23 07:05:35 -06:00
}
else if (!loc.tok){
2017-04-25 07:56:04 -06:00
console.error("❌", new Error(`Recieved an update from ${socket.ip} for usr ${loc.usr} without an sk32!`).message);
2017-04-23 07:05:35 -06:00
}
2017-03-14 07:58:56 -06:00
else {
// Get loc.usr
2017-04-20 21:07:35 -06:00
User.findById(loc.usr)
.where('sk32').equals(loc.tok)
2017-04-20 21:07:35 -06:00
.then( (user)=>{
2017-04-23 07:05:35 -06:00
if (!user){
console.error("❌", new Error(`Recieved an update from ${socket.ip} for ${loc.usr} with tok of ${loc.tok}, but no such user was found in the db!`).message);
2017-04-23 07:05:35 -06:00
}
else {
2017-03-14 07:58:56 -06:00
// Broadcast location
io.to(loc.usr).emit('get', loc);
2017-05-08 11:47:53 -06:00
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),
2017-05-06 23:12:54 -06:00
time: loc.tim
};
user.save()
.catch( (err)=>{ console.error("❌", err.stack); });
2017-05-30 12:41:23 -06:00
2017-03-14 07:58:56 -06:00
}
2017-04-20 21:07:35 -06:00
})
2017-04-25 07:56:04 -06:00
.catch( (err)=>{ console.error("❌", err.stack); });
2017-03-14 07:58:56 -06:00
}
});
// Shutdown (check for remaining clients)
socket.on('disconnect', (reason)=>{
2017-05-08 11:47:53 -06:00
debug(`${socket.id} disconnected because of a ${reason}.`);
2017-03-14 07:58:56 -06:00
// Check if client was receiving updates
if (socket.gets){
2017-05-08 11:47:53 -06:00
debug(`${socket.id} left ${socket.gets}`);
2017-03-14 07:58:56 -06:00
checkForUsers( io, socket.gets );
}
});
});
}
};