-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathIOSClientManager.js
46 lines (34 loc) · 1.69 KB
/
IOSClientManager.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
const
server_config = require('../configs/server_config'),
_ = require('lodash')
;
const IOSClientManager = function(){
// Список ключей с текущими подключениями
const connections = {};
return {
// Проверка валидности ключа и двойного подключения
connect: function(socket){
// Ключ из настроек iOS приложения
let authKey = socket.handshake.headers['authkey'];
return new Promise(function (resolve, reject) {
// Если с таким ключем уже есть подключение, то это сразу отменяем
if( _.has(connections, authKey) && connections[authKey] ) return reject('key_in_use');
if( !_.includes(server_config.DJI_KEYS, authKey) ) return reject('key_invalid');
// Ключ есть, создаем подключение
connections[authKey] = true;
socket.on("error", err => {
console.log('iOS socket error', err);
socket.disconnect();
});
// При отключении сокета, убираем ключ из списка и обновляем срок жизни на 3 часа
socket.on('disconnect', reason => { // reason
console.log("iOS app disconnected", reason);
connections[authKey] = null;
_.unset(connections, authKey);
});
return resolve(authKey);
});
}
};
}();
module.exports = IOSClientManager;