-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (140 loc) · 4.89 KB
/
server.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//server.js
var act = new require('./js/act.js').Act;
const Act = new act();
var uploader = new require('./uploader.js');
class Server{
constructor(_port){
var WebSocketServer = new require('ws');
this.wsServer = new WebSocketServer.Server({
port:_port
});
this.wsServer.on('connection',this.prosessConnection.bind(this));
this.clients = {};
console.log('Server running on port '+_port);
this.mysql = require('mysql');
this.sqlClient = this.mysql.createConnection({
host:'127.0.0.1',
port:3333,
user:'admin',
password:'IL15OHM.m',
database:'razmetka'
});
this.sqlClient.connect(this.processDbConnection.bind(this));
}
processDbConnection(err){
if(err)throw err;
console.log('database connected');
}
prosessConnection(wsocket){
var id = parseInt((new Date()).getTime());
this.clients[id] = wsocket;
console.log('Соединение ' + id + ' открыто');
wsocket.on('message',this.processMessage.bind(this,id));
wsocket.on('close',this.processClose.bind(this,id));
}
sendToAllClients(outMsg){
for(let key in this.clients){
this.clients[key].send(outMsg);
}
}
sendError(err, client_id){
console.log(err);
}
processMessage(client_id, inMsg){
console.log('CLIENT:', client_id,'\n>>', inMsg);
var onResult = function(err, result){
if(err) throw err;
let outMsgJson = JSON.parse(inMsg);
if(result.insertId) outMsgJson.data.data['id'] = result.insertId;
console.log(outMsgJson);
let outMsgStr = JSON.stringify(outMsgJson);
this.sendToAllClients(outMsgStr);
}
var processSelect = function(err, selectRes){
if(err) throw err;
let outMsgJson = JSON.parse(inMsg);
outMsgJson.data['data'] = selectRes;
let outMsg = JSON.stringify(outMsgJson);
this.clients[client_id].send(outMsg);
console.log(outMsgJson);
}
var msg = JSON.parse(inMsg).data;
try{
switch(msg.action){
case Act.LOAD:
this.select(msg, client_id, processSelect.bind(this));
console.log('> SELECT <');
break;
case Act.CREATE:
this.insert(msg, onResult.bind(this));
console.log('> INSERT <');
break;
case Act.UPDATE:
this.update(msg, onResult.bind(this));
console.log('> UPDATE <');
break;
case Act.DELETE:
this.remove(msg, onResult.bind(this));
console.log('> DELETE <');
break;
default:
this.sendError("Неизвестная команда: "+msg.action, client_id);
}
}catch(err){
this.sendError(err, client_id);
}
}
update(msg, callback){
let equations = [];
for(let key in msg.data){
if(key === '_date'){
let d = new Date(msg.data[key]).toISOString().slice(0,19).replace('T',' ');
equations.push(`${key}='${d}'`);
}else{
equations.push(`${key}='${msg.data[key]}'`);
}
}
let sql = `UPDATE ${msg.table} SET ${equations.join(',')} WHERE id=${msg.id};`;
this.sqlClient.query(sql, callback);
}
remove(msg,callback){
let sql;
sql = `DELETE FROM ${msg.table} WHERE id=${msg.id};`;
var outMsg = JSON.stringify(msg);
this.sqlClient.query(sql, callback);
}
insert(msg,callback) {
let sql;
let valuesList='', keysList='';
for(let key in msg.data){
if(key === '_date'){
let d = new Date(msg.data._date).toISOString().slice(0,19).replace('T',' ');
valuesList += `'${d}',`;
}else{
valuesList += `'${msg.data[key]}',`;
}
keysList += `${key},`;
}
keysList = keysList.slice(0,keysList.length-1);
valuesList = valuesList.slice(0,valuesList.length-1);
sql = `INSERT INTO ${msg.table} (${keysList}) VALUES (${valuesList});`;
console.log('\n'+sql+'\n');
this.sqlClient.query(sql, callback);
return {msg};
}
select(msg, id, callback) {
let sql;
if(msg.where){
sql = `SELECT * FROM ${msg.table} WHERE ${msg.where}`;
}else{
sql = `SELECT * FROM ${msg.table};`;
}
console.log(sql);
this.sqlClient.query(sql, callback);
}
processClose(id){
console.log('Соединение ' + id + ' закрыто');
delete this.clients[id];
}
}
var razmetServer = new Server(8081);