[21: 31, 22: 14, 23: 1, 24: 35, 25: 6, 26: 16, 27: 13, 28: 78, 29: 1,30: 42, 31: 21, 32: 31, 33: 15,34: 20, 35: 24]
и [[24: 35], [21: 31]]
{"21":31,"22":14,"23":1,"24":35,"25":6,"26":16,"27":13,"28":78,"29":1,"30":42,"31":21,"32":31,"33":15,"34":20,"35":24}
и {"21":31,"24":35}
соответственно. const net = require("net");
function send(event, data){
this.write(
JSON.stringify({
event: event,
data: data
})+"\n"
);
}
class Server extends EventEmitter {
constructor(options={}){
super();
this.clients = {};
this.host=options.host||'0.0.0.0';
this.port=options.port||'8020';
this.server = net.createServer ((client)=>{
client.send = send;
client.on('data', data=>{
try{
const str = data.toString();
const json = JSON.parse(""+str);
if( json && json.event && json.data!==undefined){
this.emit(json.event, json.data, client);
}
}catch(err){
console.log('\n\nError parse json', err);
}
});
client.on('close', data=>{
// ...
});
});
}
listen(host, port){
this.host=host||this.host;
this.port=port||this.port;
this.server.listen(this.port, this.host);
console.log('Server was ben started as ' + this.host + ':' + this.port);
}
}
module.exports.Server = Server;
class Client extends EventEmitter {
constructor(options={}){
super();
this.host=options.host||'192.168.0.1';
this.port=options.port||'8020';
this.client = new net.Socket();
this.client.send = send;
this.client.on('data', data=>{
data.toString().split(/[\n\r]+/).forEach(str=>{
if( !str || str.search(/^[\s\n\r]+$/)+1 ) return;
try{
const json = JSON.parse(""+str);
if( json && json.event && json.data!==undefined){
this.emit(json.event, json.data, this.client);
}
}catch(err){
console.log('\n\nError parse json ', err);
}
});
});
this.client.on('close', ()=>{
console.log('Connection closed');
this.emit("disconnect", {});
setTimeout(()=>{
this.connect()
}, 6000);
});
this.client.on('error', ()=>{
console.log('Connection error');
this.emit("disconnect", {});
this.client.destroy();
});
}
connect(host, port){
this.host=host||this.host;
this.port=port||this.port;
this.client.connect(this.port, this.host, (err)=>{
this.emit('connect', {}, this.client);
if(err) reject(err);
});
}
}
module.exports.Client = Client;
const storrage = require("./modules/storrage.js");
const users = storrage("../config/users.json",{});
const config = storrage("../config/config.server.json",{});
const NU = require("./modules/net-union.js");
const server = new NU.Server();
const clients = {};
function getUser(data){
if( !data || !data.login || !data.password )
return false;
const user = users[data.login];
if( !user || user.password != data.password)
return false;
return user;
}
server.on("avance", (data, client )=>{
const user = getUser(data.auth);
if( !user ) return client.end();
const username = data.auth.login;
clients[username] = client;
user.points-=data.avance;
client.send("points", user.points);
});
server.on("unlock", (data, client )=>{
console.log("unlock", data);
const user = getUser(data);
if( !user ) return client.end();
const username = data.login;
clients[username] = client;
user.points--;
client.send("points", user.points);
});
function update_points(username,amount) {
if( !amount ) return;
const user = users[username];
if( !user ) return;
const client = clients[username];
if( !client ) return;
client.send("points", user.points);
client.send("message", "Количество минут "+(amount<0?"уменьшено":"увеличено")+" на "+amount+".\nОсталось "+user.points+" минут");
}
server.listen(config.url.host, config.url.port);
module.exports.server = server;
module.exports.update_points = update_points;
const manage = require("./modules/manage.js");
const storrage = require("./modules/storrage.js");
const config = storrage("./config/config.client.json",{});
const NU = require("./modules/net-union.js");
const client = new NU.Client();
var count = 0;
var avance = 0;
var server;
var isConnect = false;
// при подключении к серверу отправить запрос на авторизацию
client.on("connect", (data, serv )=>{
if(isConnect) return;
isConnect = true;
server = serv;
console.log('\n\nПодключился к ' + client.host + ":" + client.port);
if(avance)
server.send("avance", {auth: config.auth, avance: avance});
});
// при отключении к серверу переходим в автономный режим
client.on("disconnect", (data)=>{
console.log("client.on disconnect");
isConnect = false;
});
// при получении количества баллов обновить счетчик
client.on("points", (points, serv )=>{
console.log("client.on points", points);
count = points;
notify();
});
// при получении сообщения - вывести уведомление
client.on("message", (message, serv )=>{
console.log("client.on message", message);
manage.notify("Таймер", message);
});
client.connect(config.url.host, config.url.port);
function notify() {
if(count>8 && count%10===0){
manage.notify("Таймер", "Осталось "+count+" минут");
}else if(count==5){
manage.notify("Таймер", "Осталось "+count+" минут");
}else if(count>1 && count<5){
manage.notify("Таймер", "Осталось "+count+" минуты");
}else if(count==1){
manage.notify("Таймер", "Осталась 1 минута");
}else if(count<1){
manage.notify("Таймер", "ВРЕМЯ ВЫШЛО");
lock()
}
}
function lock() {
setTimeout(function() {
manage.lock(config.auth.login);
}, 20000);
}
function observe_session() {
console.log("next time");
manage.isLocked(config.auth.login, isLocked=>{
if(isLocked) return;
if( isConnect ){
server.send("unlock", config.auth);
}else{
count--;
avance++;
notify();
}
});
setTimeout( observe_session , 60000 );
}
setTimeout( observe_session , 10000 );
Масштабируется:
колесиком мыши (пропорционально)
alt+колесиком мыши (по вертикали)
Перетаскивается мышкой