-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
57 lines (42 loc) · 1.24 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
const express = require('express')
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const database = require('./app/db')
app.use(express.static('public'));
database.init();
app.get('/', function (req, res) {
res.sendFile(__dirname + '/public/index.html');
});
// #### insertion pseudo ####
// ##### connexion + recuperation message #####
io.on('connection', function (socket) {
const rows = database.select(socket)
console.log('message récupéré')
// ##### deconnexion #####
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
// #### insertion des nouveaux messages ####
io.on('connection', function (socket) {
socket.on('chat message', function (msg) {
console.log('message: ' + msg);
database.insert(msg)
});
});
io.on('connection', function (socket) {
socket.on('chat message', function (msg) {
io.emit('chat message', msg);
});
});
// close the database connection
// db.close((err) => {
// if (err) {
// return console.error(err.message);
// }
// console.log('Close the database connection.');
// });
http.listen(3000, function () {
console.log('listening on *:3000');
});