Commit 51b46902 authored by Kannan V M's avatar Kannan V M
Browse files

Add logging logic

parent fa549489
......@@ -201,4 +201,7 @@ tags
# End of https://www.gitignore.io/api/vim
# config file
config.js
\ No newline at end of file
config.js
# log file
sfcamp
// const fs = require('fs');
const fs = require('fs');
const express = require('express');
......@@ -11,7 +11,6 @@ const config = require('./config');
const port = config.port || 3000;
// const filename = config.filename || 'knowledge.json';
// const filepath = path.join('/tmp', filename);
// asking server to listen to 3000
server.listen(port, () => {
......@@ -51,12 +50,30 @@ const roomLeafIds = {
hi: { leafIds: [...leafIds] },
};
const leavesData = { en: {}, ml: {}, hi: {} };
const leavesData = {
en: {},
ml: {},
hi: {},
ma: {},
};
// object of structure {socketid1: {userName:"", leafText:"",leafId:""}, ...}
const fileSave = function fileSave(roomId) {
const date = new Date();
const filename = `sfcamp_${roomId}${date.getDate()}${date.getHours()}${date.getMinutes()}`;
const filepath = path.join(__dirname, 'sfcamp', filename);
if (Object.entries(leavesData[roomId]).length !== 0) {
fs.writeFileSync(filepath, JSON.stringify(leavesData[roomId]), (err) => {
if (err) return console.error(err);
});
}
};
const notifyEveryone = function notifyUserStateChangeToEveryone(roomId) {
userNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
modNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
fileSave(roomId);
};
const updateLeavesData = function updateLeavesDataAndNotify(socketId, roomId, leafData) {
......@@ -74,6 +91,7 @@ const deleteLeaf = function deleteLeafAndNotifyEveryone(socketId, roomId) {
roomLeafIds[roomId].leafIds.push(leafId);
userNamespace.to(roomId).emit('leaf delete', leafId);
modNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
fileSave(roomId);
};
userNamespace.on('connection', (socket) => {
......@@ -94,10 +112,6 @@ userNamespace.on('connection', (socket) => {
}
updateLeavesData(socketId, roomId, leafData);
// fs.writeFileSync(filepath, JSON.stringify(leavesData[roomId]), (err) => {
// if (err) return console.error(err);
// });
});
} else {
socket.disconnect();
......@@ -125,8 +139,10 @@ modNamespace.on('connection', (socket) => {
userNamespace.sockets.get(socketId).disconnect();
delete leavesData[roomId][socketId];
modNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
fileSave(roomId);
});
socket.on('save exit', () => { // save data into file
fileSave(roomId);
userNamespace.in(roomId).disconnectSockets();
Object.keys(leavesData[roomId]).forEach((key) => {
delete leavesData[roomId][key];
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment