-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
68 lines (55 loc) · 1.75 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
const express = require("express");
const http = require("http");
const socketIO = require("socket.io");
const mysql = require("mysql");
// our localhost port
const port = 2222;
const app = express();
// our server instance
const server = http.createServer(app);
// This creates our socket using the instance of the server
const io = socketIO(server);
let mySQL_status = false;
//MySql Database connection
//LEARM : MYSQL CHEAT SHEET : http://www.newthinktank.com/2014/08/mysql-video-tutorial/
var db = mysql.createConnection({
host: "localhost",
user: "mysqladmin",
password: "admin",
database: "chat_app"
});
db.connect(err => {
if (err) throw err;
console.log("MySQL Connected!");
mySQL_status = true;
});
//What to do when the socket is connected to a client
io.on("connection", socket => {
if (mySQL_status) {
//TODO: 1 - This sends all uses all conversations whenever anybody connectiosn
//TODO: 2 - io.clients[] Is what i should learn and use apparently
// Do this when the a user connects
socket.on("user_connect", user =>
db.query(`SELECT * FROM conversations`, (err, result, fields) => {
if (err) throw err;
io.sockets.emit("conversations", result);
})
);
// When new Message => Send Message back out!
socket.on("new_msg", msg => {
//Insert message into messages
db.query(
`INSERT INTO messages VALUES(NULL, 1, NOW(), '${msg}', 1)`,
(err, result, fields) => {
if (err) throw err;
}
);
io.sockets.emit("new_msg", msg);
});
}
// When user disconnects => log disconnect
socket.on("user_disconnect", user =>
console.log(`User: ${user} disconnected.`)
);
});
server.listen(port, () => console.log(`Listening on port ${port}`));