-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
58 lines (47 loc) · 1.44 KB
/
server.js
File metadata and controls
58 lines (47 loc) · 1.44 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
// express setup
const bodyParser = require('body-parser');
var express = require('express');
var app = express();
const port = 8080;
// socket.io setup
var http = require('http').Server(app);
var io = require('socket.io')(http);
// mongo setup
const MongoClient = require('mongodb').MongoClient;
const db_cfg = require('./config/db');
var ObjectID = require('mongodb').ObjectID;
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/chat', function(req, res, next) {
res.sendFile(__dirname + '/public/chat.html')
});
app.use(express.static('public'));
MongoClient.connect(db_cfg.url, (err, database) => {
if (err) return console.log(err)
var db = database.db(db_cfg.title)
require('./app/routes')(app, db);
io.on('connection', function(socket){
socket.on('join', function(data) {
console.log(data);
userCounter(db, id, 1);
});
var id = "5b0073b9ae8b8416e8fc5bcf";
socket.on('chat message', function(msg){
socket.broadcast.emit('chat message', msg);
});
socket.on('disconnect', function(){
userCounter(db, id, -1);
});
});
http.listen(port, () => {
console.log('We are live on ' + port);
});
})
function userCounter(db, id, val) {
const details = { '_id': new ObjectID(id) };
const chat = { $inc: { users: val } };
db.collection('chats').update(details, chat, (err, result) => {
if (err) {
console.log({'error':'An error has occurred'});
}
});
};