-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (134 loc) · 3.75 KB
/
index.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
var express = require("express");
var WebSocketServer = new require('ws');
var app = express();
app.disable('x-powered-by');
app.use(express.static('public'));
app.get('*', function(req,res) {
res.status(404).send('Такой страницы не существует, перейдите на <a href="/">главную</a>');
});
app.listen(8090, function(){
console.log('Open http://127.0.0.1:8090/');
});
var History = function() {
var hist = {};
var getTime = function() {
return new Date().getTime();
};
return {
get: function() {
return JSON.stringify(hist);
},
addUser: function(name) {
hist[getTime()] = {
type: 'newUser',
data: xss(name)
}
},
delUser: function(name) {
hist[getTime()] = {
type: 'leaveUser',
data: xss(name)
}
},
addMsg: function(name, text) {
hist[getTime()] = {
type: 'newMsg',
data: text,
name: xss(name)
}
}
}
};
var history = new History();
var xss = function(str) {
if (typeof str != 'string') {
return str;
}
var replaceAll = function(strreg, find, replace) {
return strreg.replace(new RegExp(find, 'g'), replace);
};
str = replaceAll(str, '<', '<');
str = replaceAll(str, '>', '>');
str = replaceAll(str, '"', '“');
str = replaceAll(str, '\'', '‘');
return str;
};
var clients = {};
var clientsNames = new Array();
var webSocketServer = new WebSocketServer.Server({
port: 9000
});
webSocketServer.on('connection', function(ws) {
var id = parseInt(Math.random().toString().slice(0,10) * 10000);
clients[id] = ws;
ws.on('message', function(msg) {
if (msg.length > 0) {
msg = JSON.parse(msg);
if (msg.type != undefined && msg.type.length > 0 && msg.data != undefined && msg.data.length > 0) {
switch (msg.type) {
case 'checkName':
if (clientsNames.indexOf(msg.data) !== -1) {
ws.send(JSON.stringify({
type: 'checkName',
data: false
}));
delete clients[id];
}
else {
clientsNames[id] = msg.data;
history.addUser(msg.data);
var userList = clientsNames.reduce(function(o, v, i) {
o[i] = xss(v);
return o;
}, {});
ws.send(JSON.stringify({
type: 'checkName',
data: true,
users: userList,
history: history.get()
}));
for (var key in clients) {
if (key != id) {
clients[key].send(JSON.stringify({
type: 'newUser',
data: xss(msg.data),
users: userList
}));
}
}
}
break;
case 'newMsg':
history.addMsg(clientsNames[id], msg.data);
for (var key in clients) {
clients[key].send(JSON.stringify({
type: 'newMsg',
name: xss(clientsNames[id]),
data: xss(msg.data)
}));
}
break;
}
}
}
});
ws.on('close', function() {
delete clients[id];
if (clientsNames[id] != undefined && clientsNames[id].length > 0) {
var leaveUserName = clientsNames[id];
history.delUser(leaveUserName);
delete clientsNames[id];
for (var key in clients) {
var userList = clientsNames.reduce(function(o, v, i) {
o[i] = xss(v);
return o;
}, {});
clients[key].send(JSON.stringify({
type: 'leaveUser',
data: xss(leaveUserName),
users: userList
}));
}
}
});
});