-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchat.js
62 lines (48 loc) · 1.52 KB
/
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
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
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var mysql = require('mysql');
var port = process.env.PORT || 3000;
var historyCount = 100;
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "12345",
database: "toshe-api"
});
con.connect(function(err) {
if (err) throw err;
console.log("MySQL connected!");
});
var getTimestamp = function() {
return Math.floor(new Date().getTime()/1000);
}
var message = function(user, text) {
return {"user": user, "text": text, "time": getTimestamp()};
}
// TODO: Filter msg, SQL Injection
io.on('connection', function(socket){
socket.on('chat', function(msg){
msg["time"] = getTimestamp();
var query = con.query('INSERT INTO chat SET ?', msg, function (error, results, fields) {
if (error) throw error;
io.emit('chat', msg);
});
});
socket.on('joined', function (user) {
// Send joined message
io.emit('joined', message(user, "Joined the chat"));
// Send chat history to user
con.query("SELECT * FROM chat ORDER BY time DESC LIMIT " + historyCount, function (err, result) {
if (err) throw err;
socket.emit('history', result);
});
});
socket.on('disconnect', function () {
// Send left message to others
io.emit('left', message("SOMEONE", "Left the chat"));
});
});
http.listen(port, function(){
console.log('listening on *:' + port);
});