This repository has been archived by the owner on Apr 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (77 loc) · 2.16 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
const express = require('express');
const app = express();
const http = require('http');
const mysql = require('mysql')
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
var con = mysql.createConnection({
host: 'sql12.freemysqlhosting.net',
user: 'sql12561191',
database: 'sql12561191',
password: process.env.DB_PASS
});
con.connect(function (err) {
if (err) throw err;
console.log("Connected");
});
app.get('/style.css', (req, res) => {
res.sendFile(__dirname + '/style.css');
})
app.get('/img/0000001.jpg', (req, res) => {
res.sendFile(__dirname + '/img/0000001.jpg');
})
app.get('/img/default-pfp.png', (req, res) => {
res.sendFile(__dirname + '/img/default-pfp.png');
})
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
/*
app.get("/:username/chat", (req, res, next) => {
req.username = req.params.username;
next()
}, (req, res, next) => {
res.send({
echo: req.username
})
})
*/
io.on('connection', (socket) => {
socket.on('user-connect', (username) => {
console.log(`user ${username} connected`);
io.emit('user-connect', username);
})
socket.on('disconnect', () => {
console.log('user disconnected');
});
});
io.on('connection', (socket) => {
socket.on('message-submit', (messageDetails) => {
io.emit('message-submit', messageDetails);
console.log(messageDetails);
let username = messageDetails.username;
let message = messageDetails.message;
let date = messageDetails.date;
var sql = "insert into g4o2 (username, message, message_date) values (?, ?, ?)";
con.query(sql, [username, message, date], function(err, result) {
if (err) throw err;
console.log("1 record inserted");
});
// con.end();
});
})
io.on('connection', (socket) => {
socket.on('load-messages', (username) => {
console.log(`user ${username} load chat`)
con.query('SELECT * from g4o2', (err, rows, fields) => {
if (err) throw err
rows = rows.map(v => Object.assign({}, v));
io.emit('load-messages', rows);
console.log(rows);
})
})
})
server.listen(3000, () => {
console.log('listening on *:3000');
});