-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbServer.js
186 lines (157 loc) · 6.08 KB
/
dbServer.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
const express = require('express');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const http = require('http');
const { Server } = require('socket.io');
const WebSocket = require('ws');
const url = require('url');
const wss = new WebSocket.Server({ noServer: true });
const rooms = {}; // Format: { roomName: { client1: username1, client2: username2, ... }, ... }
const server = http.createServer();
const app = express();
app.use(bodyParser.json());
const io = new Server(server);
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/index.html');
});
require("dotenv").config()
const DB_HOST = process.env.DB_HOST
const DB_USER = process.env.DB_USER
const DB_PASSWORD = process.env.DB_PASSWORD
const DB_DATABASE = process.env.DB_DATABASE
const DB_PORT = process.env.DB_PORT
const db = mysql.createPool({
connectionLimit: 100,
host: DB_HOST,
user: DB_USER,
password: DB_PASSWORD,
database: DB_DATABASE,
port: DB_PORT
})
let hosts = {};
// Timer to refresh database values every 20 seconds
setInterval(() => {
db.query('SELECT * FROM conference WHERE endTime is null', (error, results) => {
if (error) {
console.error('Error fetching data: ', error);
return;
}
let updatedHosts = {};
results.forEach(row => {
updatedHosts[row.roomId] = {
roomId: row.roomId,
id: row.hostUserId,
startTime: row.startTime,
endTime: row.endTime
};
});
hosts = updatedHosts;
io.emit('updateHosts', hosts);
console.log('Hosts updated:', hosts);
});
}, 20000);
server.on('upgrade', function upgrade(request, socket, head) {
const pathname = url.parse(request.url).pathname;
if (pathname === '/hosts/') {
wss.handleUpgrade(request, socket, head, function done(ws) {
ws.send(JSON.stringify(hosts));
});
} else if (pathname.startsWith('/chatroom/')) {
const roomName = pathname.split('/')[2];
wss.handleUpgrade(request, socket, head, function done(ws) {
let username = 'User' + Date.now() + Math.floor(Math.random() * 1000);
ws.username = username;
ws.room = roomName;
if (!rooms[roomName]) {
rooms[roomName] = new Map();
}
rooms[roomName].set(ws, username);
wss.emit('connection', ws, request);
});
} else {
socket.destroy();
}
});
wss.on('connection', function connection(ws) {
console.log(`${ws.username} joined room: ${ws.room}`);
ws.on('message', function incoming(message) {
// Broadcast message to all clients in the same room
const room = ws.room;
if (rooms[room]) {
rooms[room].forEach((username, client) => {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(`${ws.username}: ${message}`);
}
});
}
});
ws.on('close', function close() {
if (ws.room && rooms[ws.room]) {
rooms[ws.room].delete(ws);
console.log(`${ws.username} left room: ${ws.room}`);
}
});
ws.on('error', function error(err) {
console.error('WebSocket error:', err);
});
});
// Utility function to generate OTP
const generateOTP = () => {
return Math.floor(1000 + Math.random() * 9000); // 4-digit number
};
// OTP generator API
app.post('/otp-generate', (req, res) => {
const { phoneNumber } = req.body;
const phoneNumberPattern = /^\d{10}$/;
if (!phoneNumber) {
return res.status(400).send({ success: false, message: 'Mobile number is required' });
}
if (!phoneNumberPattern.test(phoneNumber)) {
return res.status(400).send({ success: false, message: 'Enter a valid mobile number' });
}
const otp = generateOTP();
res.status(200).send({ success: true, message: `Your one time password is generated.`, otp: otp });
});
// User registration API
app.post('/user-registration', (req, res) => {
const { email, userName, userProfilePic, phoneNumber, address, androidId, fcmToken, dateOfBirth } = req.body;
const query = 'SELECT * from user WHERE email = ? OR phoneNumber = ?';
db.query(query, [email, phoneNumber], (error, results) => {
if (error) {
return res.status(500).send({ success: false, message: 'Error querying the database', error });
}
if (results.length) {
return res.status(400).send({ success: false, message: 'User already exists, redirecting to login page' });
}
const newUser = {
email, userName, userProfilePic, phoneNumber, address, androidId, fcmToken, dateOfBirth, userStatus: 0, isUserBlocked: false
};
db.query('INSERT INTO user SET ?', newUser, (error, results) => {
if (error) {
return res.status(500).send({ success: false, message: 'Error inserting data into the database', error });
}
newUser['userId'] = results.insertId;
return res.status(200).send({ success: true, message: 'User registered successfully', user: { newUser } });
});
});
});
// User login API
app.post('/user-login', (req, res) => {
const { email, phoneNumber } = req.body;
const query = 'SELECT * FROM user WHERE email = ? OR phoneNumber = ?';
db.query(query, [email, phoneNumber], (error, results) => {
if (error) {
return res.status(500).send({ success: false, message: "Error querying the database", error });
}
if (results.length) {
return res.status(200).send({ isUserRegistered: true, success: true, message: 'Logging in', user: results[0] });
} else {
return res.status(200).send({ isUserRegistered: false, success: false, message: 'User does not exist' });
}
})
})
const port = process.env.PORT || 3000;
// app.get('/', (req, res) => {
// res.send("Server is running!");
// });
server.listen(port, () => console.log(`Server Started on port ${port}...`)).setMaxListeners(10);