-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
105 lines (73 loc) · 2.14 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const http=require('http');
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const passport = require('passport');
const path = require('path');
const socketIo=require('socket.io');
const app = express();
var server = require('http').Server(app);
const io=socketIo(server);
// Body parser middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// DB Config
const db = require('./config/keys').mongoDB;
// Passport middleware
app.use(passport.initialize());
// Passport Config
require('./config/passport')(passport);
app.use(function(req,res,next){
req.io = io;
next();
});
//routes
const auth=require('./routes/auth/auth');
const home=require('./routes/home/home');
const post=require('./routes/post/post');
const profile=require('./routes/profile/profile');
const search=require('./routes/search/search');
const util=require('./routes/friendsystem/friendsystem');
// Connect to MongoDB
mongoose
.connect(db)
.then(() => console.log('MongoDB Connected'))
.catch(err => console.log(err));
//route middleware
app.use('/auth',auth);
app.use('/',home);
app.use('/profile',profile);
app.use('/post',post);
app.use('/search',search);
app.use('/util',util);
// Server static assets if in production
if (process.env.NODE_ENV === 'production') {
// Set static folder
app.use(express.static('client/build'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
}
const port = process.env.PORT || 5000;
server.listen(port, () => console.log(`Server running on port ${port}`));
let onlineClients={};
io.sockets.on('connection', function (socket) {
socket.on('adduser',(data)=>{
onlineClients[data.id] = {
"socket": socket.id
};
})
//Removing the socket on disconnect
socket.on('disconnect', function() {
for(var name in onlineClients) {
if(onlineClients[name].socket === socket.id) {
delete onlineClients[name];
break;
}
io.emit('checkOnline');
}
})
io.myclients=onlineClients;
io.emit('checkOnline');
console.log("new client");
})