-
Notifications
You must be signed in to change notification settings - Fork 7
/
chat.js
33 lines (27 loc) · 852 Bytes
/
chat.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
var Chat = exports = module.exports = {};
var Pool = require("./client");
var index = 0;
var lastAccessed = 0;
var char_history = {};
var io;
function htmlEscape(str) {
return String(str)
.replace(/&/g, '&')
.replace(/"/g, '"')
.replace(/'/g, ''')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/\//, '/');
}
Chat.addMessage = function (from, to, message) {
message = htmlEscape((message));
if (to == null) {
io.sockets.emit("chat", {from: from, message: message, private: false});
} else {
Pool.get_client_by_name(to).emit("chat", {from: from, message: message, private: true})
Pool.get_client_by_name(from).emit("chat", {from: from, message: message, private: true})
}
}
Chat.initialize = function (server) {
io = server;
}