-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
81 lines (71 loc) · 2.47 KB
/
app.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
require('dotenv').config()
const chats = require('./models/chats')
const app = require('express')();
var port_number = process.env.PORT || 4000;
const server = app.listen(port_number);
const io = require('socket.io')(server);
const mongoose = require('mongoose');
const cors = require('cors');
const mongoURL = `mongodb://devansh:${process.env.mongoPass}@anonymous-project-shard-00-00-e0dq9.mongodb.net:27017,anonymous-project-shard-00-01-e0dq9.mongodb.net:27017,anonymous-project-shard-00-02-e0dq9.mongodb.net:27017/test?ssl=true&replicaSet=Anonymous-Project-shard-0&authSource=admin&retryWrites=true&w=majority`;
app.use(cors());
mongoose.connect(mongoURL, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
useFindAndModify: false
}).then(() => {
console.log('MongoDB is connected')
}).catch(err => {
console.log(`MongoDB connection unsuccessful due to ${err}, retry after 5 seconds.`)
setTimeout(connectWithRetry, 5000)
})
var connectWithRetry = function () {
return mongoose.connect(mongoURL, function (err) {
if (err) {
console.error('Failed to connect to mongo on startup - retrying in 5 sec', err);
setTimeout(connectWithRetry, 5000);
}
});
};
io.sockets.on('connection', function (socket, client) {
socket.on('room', function (room) {
socket.join(room);
console.log(`Someone joined on ${room}`)
async function chatsfun(room) {
const nchats = await chats.find({ roomno: room })
if (nchats) {
return nchats
} else {
return [{ username: 'Admin', chat: `welcome to room number ${req.params.roomno}` }]
}
}
async function emit(room) {
const nchats = await chatsfun(room);
socket.emit('chats', nchats)
}
emit(room)
})
socket.on("newMessage", async (data) => {
try {
var chat = new chats({
username: data.username,
chat: data.chat,
roomno: data.roomno
})
await chat.save();
io.sockets.in(data.roomno).emit('newChat', data)
} catch{
console.log('Error')
}
})
})
app.get('/', function (req, res) {
res.redirect('https://www.github.com/DevanshCodes')
})
app.get('/api/port', async function (req, res) {
try {
return res.json(port_number)
} catch (error) {
console.log(error);
}
})