Skip to content

Commit

Permalink
changed socket connection endpoint and added isConnected method
Browse files Browse the repository at this point in the history
  • Loading branch information
hardikroongta8 committed Mar 29, 2024
1 parent 30c62fd commit d42f413
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
11 changes: 10 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const securityKeyMiddleware = require("./middlewares/securityKeyMiddleware");

const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const wss = new WebSocket.Server({ server , path: '/ws'});

app.use(express.json());
app.use(securityKeyMiddleware);
Expand All @@ -28,6 +28,15 @@ exports.sendMessageToSocket = (connectionId, data) => {
});
}

exports.isConnected = (connectionId) => {
wss.clients.forEach((client) => {
if(client.connectionId === connectionId){
return true;
}
});
return false;
}

exports.closeConnection = (connectionId) => {
wss.clients.forEach((client) => {
if(client.connectionId === connectionId){
Expand Down
22 changes: 17 additions & 5 deletions controllers/khokhaEntryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const KhokhaEntryModel = require('../models/KhokhaEntryModel');
// TODO: Implement these controllers
exports.addNewEntry = async (req, res, next) => {
try {
if(ws.isConnected(req.body.connectionId)===false){
return res.json({
success: false,
message: "Socket is not connected"
});
}
const entry = await KhokhaEntryModel.create({
outlookEmail: req.body.outlookEmail,
name: req.body.name,
Expand All @@ -17,11 +23,11 @@ exports.addNewEntry = async (req, res, next) => {
destination: req.body.destination,
});

console.log(entry);

ws.sendMessageToSocket(req.body.connectionId, {
success: true,
message: "Entry Added to database!",
entryId: entry._id
data: entry
});
ws.closeConnection(req.body.connectionId);

Expand All @@ -37,6 +43,13 @@ exports.addNewEntry = async (req, res, next) => {
exports.closeEntry = async (req, res, next) => {
const entryId = req.params.id;
try {
if(ws.isConnected(req.body.connectionId)===false){
return res.json({
success: false,
message: "Socket is not connected"
});
}

const entry = await KhokhaEntryModel.findById(entryId);

if (!entry) {
Expand Down Expand Up @@ -64,19 +77,18 @@ exports.closeEntry = async (req, res, next) => {
});
}

const response = await KhokhaEntryModel.findByIdAndUpdate(entryId, {
const newEntry = await KhokhaEntryModel.findByIdAndUpdate(entryId, {
inTime: Date(),
isClosed: true
}, {new: true});

ws.sendMessageToSocket(req.body.connectionId, {
success: true,
message: "Entry Closed Successfully!",
data: newEntry
});
ws.closeConnection(req.body.connectionId);

console.log(response);

return res.json({
success: true,
message: "Entry Closed Successfully!"
Expand Down

0 comments on commit d42f413

Please sign in to comment.