-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatserver.js
More file actions
125 lines (111 loc) · 3.57 KB
/
chatserver.js
File metadata and controls
125 lines (111 loc) · 3.57 KB
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
var simpleserver = require('./simpleserver').simpleserver;
var _ = require('underscore');
function chatserver(sqlInstance) {
this.port = 8070;
this.users = {};
this.alias = "SERVER@CHAT:"+this.port;
this.sql = sqlInstance;
}
chatserver.prototype.init = function() {
var scope = this;
this.server = new simpleserver(this.port, {
alias: "SERVER@CHAT:"+this.port, // To display on the console
onConnect: function(client) {
scope.log("Connected.","Client ID:" + client.uid, "Online on this instance:" + scope.server.ocount);
// Send auth request: Auth and update on the number of users online
scope.server.send(client.uid, [
"auth"
]);
},
onReceive: function(client, data) {
if (data != "ping" && data != "\"ping\"") {
scope.log("Message from #"+client.uid+":", data);
}
switch (data) {
case "ping":
case "\"ping\"":
scope.server.send(client.uid, "pong");
break;
default:
if (data.auth) {
// If the user provides an authtoken
if (data.auth.authtoken) {
// Save the authtoken
client.authtoken = data.auth.authtoken;
// Check Auth Token validity in the database
// {"auth":{"authtoken":"a2b8cc905c56f09007177b5df8f00e44"}}
scope.sql.query("select t.id,t.validity,t.token,t.uid,u.firstname,u.lastname,u.avatar_small from authtokens as t, users as u where u.id=t.uid and t.token='"+client.authtoken+"' and t.validity > "+(new Date().getTime()/1000), function(err, rows, fields) {
if (err) throw err;
if (rows.length > 0 && rows[0].id > 0) {
scope.log("Auth Token validated: ","UID: \t\t"+rows[0].uid,"Token: \t"+rows[0].token,"Validity: \t"+new Date(rows[0].validity*1000).toISOString());
// Register the user
scope.users[client.uid] = {
token: client.uid,
uid: rows[0].uid,
authtoken: rows[0].token,
validity: rows[0].validity,
name: rows[0].firstname,
avatar: rows[0].avatar_small
};
// Share the user with all clients
scope.server.broadcast({
online: scope.server.ocount,
newuser: scope.users[client.uid]
}, [client.uid]);
// Welcome the user
scope.server.send(client.uid, {
message: "Welcome "+scope.users[client.uid].name+"!",
from: "Entity",
identity: scope.users[client.uid],
users: scope.users
});
} else {
// Send the 'invalid_token' error to the user
scope.server.send(client.uid, {
invalid_token: client.authtoken
});
}
});
}
}
if (data.message) {
if (scope.users[client.uid]) {
scope.server.broadcast({
message: data.message,
from: client.uid
}, []);
}
}
if (data.sysmessage) {
if (scope.users[client.uid]) {
scope.server.broadcast({
sysmessage: data.sysmessage,
from: client.uid
}, []);
}
}
break;
}
},
onQuit: function(client) {
scope.log("Client Quit:", client.uid);
// remove the user from the list
delete scope.users[client.uid];
scope.server.broadcast({
quit: client.uid,
online: scope.server.ocount
});
}
});
}
chatserver.prototype.log = function(data) {
var red, blue, reset;
red = '\u001b[35m';
blue = '\u001b[34m';
reset = '\u001b[0m';
console.log(red+"<"+this.alias+">");
for (i in arguments) {
console.log(reset, arguments[i],reset);
}
}
exports.chatserver = chatserver;