/var/www/myprog.com.ua/server/node
Edit: /var/www/myprog.com.ua/server/node/session_manager.js (5688B)
const Session = require('./objects/session');
const log = require('./helpers/log');
class SessionManager {
constructor(io) {
this.io = io;
this.sessions = []; // sessions[socket_id] = session
this.sockets = []; // sockets[socket_id] = client_key
this.hosts = []; // hosts[socket_id] = socket_id
this.devices = []; // devices[host_id]= socket_id
this.users = []; // users[client_id]=socket_id
}
add_socket(socket_id, client_key) {
this.sockets[socket_id] = client_key;
}
remove_socket(socket_id) {
// удалить авто
// try {
// if (this.sessions[socket_id].client.last_car_id) {
// this.send_to_all_operators(WHO.OPERATOR.PREFIX + 'car_status',
// JSON.stringify({
// id: this.sessions[socket_id].client.last_car_id,
// on: 0
// }));
// delete this.sessions[socket_id].client.last_car_id;
// //this.cars.splice(this.sessions[socket_id].user.last_car_id, 1);
// }
// } catch (err) {
// // session not user
// }
// удалить сессию
if (this.sessions[socket_id]) {
//this.sessions.splice(socket_id, 1);
delete this.sessions[socket_id]
}
// удалить сокет
//this.sockets.splice(socket_id, 1);
delete this.sockets[socket_id];
}
add_user(session) {
//session.type = WHO.OPERATOR.VALUE;
this.sessions[session.socket.id] = session;
this.users[session.user.id] = session.socket.id;
}
add_host(session) {
//session.type = WHO.DRIVER.VALUE;
this.sessions[session.socket.id] = session;
this.hosts[session.host.id] = session.socket.id;
}
add_device(socket_id, device_id) {
//console.log("add_car car_id=" + car_id + " socket_id=" + socket_id);
this.device[device_id] = socket_id;
}
get_host_id(socket) {
return this.sessions[socket.client.id].host.id
}
add_to_room(room, session) {
session.socket.join(room);
}
get_session_by_device(device_id) {
return this.sessions[this.devices[device_id]];
}
get_session_by_socket(socket_id) {
return this.sessions[socket_id];
}
get_sessions() {
return this.sessions;
}
get_sockets() {
return this.sockets;
}
get_driver(id) {
try {
return this.sessions[this.drivers[id]].client;
} catch (e) {
return null;
}
}
get_host(id) {
try {
return this.sessions[this.hosts[id]].host;
} catch (e) {
return null;
}
}
get_user(id) {
try {
return this.sessions[this.users[id]].client;
} catch (e) {
return null;
}
}
send_to_room(room, action, message) {
//log.debug('SessionManager', 'send_to_room()', action, message);
this.io.sockets.in(room).emit(action, message);
}
send_to_user(id, action, message) {
this.sessions[this.users[id]].socket.emit(action, message);
}
send_to_all_users(action, message) {
try {
//log.debug('SessionManager', 'send_to_all_users()', action);
this.send_to_room(WHO.USER.VALUE, action, message);
} catch (err) {
log.error('SessionManager', 'send_to_all_users()', err);
}
}
send_to_device(id, action, message) {
this.sessions[this.drivers[id]].socket.emit(action, message);
}
send_to_all_devices(action, message) {
try {
if (action == 'jparks') {
log.debug('SessionManager', 'send_to_all_drivers()', 'action = ' + action + ' message = ' + (message).length + ' b');
} else {
log.debug('SessionManager', 'send_to_all_drivers()', 'action = ' + action + ' message = ' + message);
}
this.send_to_room(WHO.DEVICE.VALUE, action, message);
} catch (err) {
log.error('SessionManager', 'send_to_all_drivers()', err);
}
}
send_to_car(car_id, action, message) {
try {
log.debug('SessionManager', 'send_to_car()', `car_id = ${this.cars[car_id]}`, action, message);
this.io.sockets.in(this.cars[car_id]).emit(action, message);
} catch (err) {
log.error('SessionManager', 'send_to_car()', err);
}
}
send_to_host(host_id, action, message) {
try {
log.debug('SessionManager', 'send_to_host()', `host_id = ${this.hosts[host_id]}`, action, message);
this.io.sockets.in(this.hosts[host_id]).emit(action, message);
} catch (err) {
log.error('SessionManager', 'send_to_host()', err);
}
}
send_to_socket(socket, action, message) {
socket.emit(action, message);
}
send_to_all(action, message) {
log.debug('SessionManager', 'send_to_all()', action, message);
//this.io.sockets.emit(action, message);
this.send_to_room(WHO.DEVICE.VALUE, action, message);
this.send_to_room(WHO.USER.VALUE, action, message);
}
send_to_admins(action, message) {
try {
//log.debug('SessionManager', 'send_to_admins()', action, message);
this.send_to_room(WHO.ADMIN.VALUE, action, message);
} catch (err) {
log.error('SessionManager', 'send_to_admins()', err);
}
}
}
module.exports = SessionManager;