-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
118 lines (93 loc) · 3.66 KB
/
server.js
File metadata and controls
118 lines (93 loc) · 3.66 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
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
113
114
115
116
117
118
const express = require('express');
const mongoose = require('mongoose');
const http = require('http');
const socketio = require('socket.io');
const path = require('path');
// config variables.
const config = require('./src/config/config');
// Local Utilities.
const constants = require('./utils/constants');
const utility = require('./utils/utils');
// DB operation methods.
const controller = require('./src/controller/controller');
const app = express();
const server = http.createServer(app); // http server variable.
// Static Resources
app.use(express.static(path.join(__dirname, 'public')));
// Database
mongoose.Promise = global.Promise;
mongoose.connect(config.dbURL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => {
console.log('Database connected.!');
})
.catch((err) => {
console.log('Error connecting to database...', err)
});
const io = socketio(server);
// Socket communcation channel.
io.on('connection', socket => {
// Join room
socket.on('joinRoom', ({username, room}) => {
socket.join(room);
// Add user to DB.
let user = controller.addUser(socket.id, username, room);
// Connection success message to client.
socket.emit('messageChannel', utility.FormatMessage(constants.QuickBot, 'Quick-Chat team welcomes you.!'));
// broadcast new user join message in a room.
let broadcastMessage = `${user.username} joined the room.`;
socket.broadcast.to(user.room).emit('messageChannel', utility.FormatMessage(user.username, broadcastMessage));
controller.findUsers()
.then((users) => {
io.to(user.room).emit('userRoom', {
room: user.room,
users: users
});
})
.catch((err) => {
});
});
// Socket to receive and send message back to client.
socket.on('userMsgChannel', (userMessage) => {
controller.getUser(socket.id)
.then((user) => {
io.to(user.room).emit('messageChannel', utility.FormatMessage(user.username, userMessage));
})
.catch((err) => {
console.log('Some error occurred while sending messages... \n', err)
});
});
// Leave room.
socket.on('disconnect', () => {
controller.getUser(socket.id)
.then((user) => {
user && io.to(user.room).emit('messageChannel', utility.FormatMessage(constants.QuickBot, `${user.username} left the room.`));
// Remove user and send room info to client.
controller.removeUser(socket.id)
.then(() => {
controller.findUsers()
.then((users) => {
user && io.to(user.room).emit('userRoom', {
room: user.room,
users: users
});
})
.catch((err) => {
console.log('Sever: Some error occurred while fetching users... \n',err);
});
})
.catch((err) => {
console.log('Server: Some error occurred while fetching users... \n', err);
});
})
.catch((err) => {
console.log('Server: Some error occurred while leaving room... \n', err)
});
});
});
const port = config.PORT;
server.listen(port, () => {
console.log(`Server started at port: ${port}`);
});