-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
112 lines (105 loc) · 3.61 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
106
107
108
109
110
111
112
const http = require('http');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const { static } = require('express');
const taskSchedule = require('./utils/taskSchedule');
// const chitChat = require('./utils/chitChat');
const socketJs = require('socket.io');
process.on('uncaughtException', (err) => {
console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
console.log(err.name, err.message);
process.exit(1);
});
dotenv.config({ path: `${__dirname}/config.env` });
const app = require(`${__dirname}/app.js`);
app.set('view engine', 'ejs');
app.set('view engine', 'pug');
const DB = process.env.DATABASE.replace('<password>', process.env.PASSWORD);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
useUnifiedTopology: true,
})
.then(() => {
console.log('Database Successfully Connected');
taskSchedule();
});
const port = process.env.PORT || 3000;
const socketServer = http.createServer(app);
const server = socketServer.listen(port, () => {
console.log(`App running on port ${port}...`);
});
const io = require('socket.io')(socketServer);
const usersMapWithClientIdAndUserId = {};
const usersMapWithUserIdAndClientId = {};
io.on('connection', (client) => {
console.log('connection established');
client.on('joinRoom', (room) => {
client.join(room.roomId);
usersMapWithClientIdAndUserId[client.id] = {
userId: room.userId,
roomId: room.roomId,
};
usersMapWithUserIdAndClientId[room.userId] = client.id;
});
client.on('leaveRoom', (room) => {
client.leave(room);
const userId = usersMapWithClientIdAndUserId[client.id];
const roomId =
usersMapWithClientIdAndUserId[client.id] &&
usersMapWithClientIdAndUserId[client.id].roomId;
delete usersMapWithClientIdAndUserId[client.id];
delete usersMapWithUserIdAndClientId[userId];
client.to(roomId).emit('statusOnOffTyping', {
roomId: roomId,
statusOnOffTyping: '',
});
});
client.on('disconnect', (data) => {
const userId = usersMapWithClientIdAndUserId[client.id];
const roomId =
usersMapWithClientIdAndUserId[client.id] &&
usersMapWithClientIdAndUserId[client.id].roomId;
delete usersMapWithClientIdAndUserId[client.id];
delete usersMapWithUserIdAndClientId[userId];
client.to(roomId).emit('statusOnOffTyping', {
roomId: roomId,
statusOnOffTyping: '',
});
});
client.on('statusOnOffTyping', (data) => {
let statusOnOffTyping = data.statusOnOffTyping || '';
if (!data.statusOnOffTyping && usersMapWithUserIdAndClientId[data.userId]) {
statusOnOffTyping = 'Online';
client.emit('statusOnOffTyping', {
roomId: data.roomId,
statusOnOffTyping,
});
}
// console.log('hello,up', statusOnOffTyping, data);
client.to(data.roomId).emit('statusOnOffTyping', {
roomId: data.roomId,
statusOnOffTyping,
});
});
client.on('recieveChatFromClient', (data) => {
client.to(data.roomId).emit('recieveChatFromClientFriend', {
chatFromServer: data.chatFromYou,
forRoomId: data.roomId,
chatId: data.chatId,
createdDate: data.createdDate,
});
});
client.on('requestToDeleteChatFromClient', (data) => {
client.to(data.roomId).emit('recieveRequestToDeleteChatFromFriend', data);
});
});
process.on('unhandledRejection', (err) => {
console.log('UNHANDELED REJECTION! 💥 Shutting down...');
console.log(err.name, err.message);
server.close(() => {
process.exit(1);
});
});