Skip to content
Snippets Groups Projects
Commit 5aeb8360 authored by Kannan V M's avatar Kannan V M
Browse files

Refactor server side code

parent b44b841d
Branches
1 merge request!5Add multi room support and moderation
var config = require('./config');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var path = require('path');
var fs = require('fs')
var port = config.port || 3000;
var filename = config.filename || 'knowledge.json';
var filepath = path.join('/tmp', filename);
//asking server to listen to 3000
// const fs = require('fs');
const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const path = require('path');
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, () => {
console.log('Server listening at port %d', port);
console.log('Server listening at port %d', port);
});
//assign static path to public folder
// assign static path to public folder
app.use(express.static(path.join(__dirname, '/public/')));
app.get('/mod_en', (req, res) => {
res.sendFile(__dirname + '/public/mod.html');
res.sendFile(path.join(__dirname, '/public/mod.html'));
});
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
res.sendFile(path.join(__dirname, '/index.html'));
});
const user_en = io.of('/user_en');
const mod_en = io.of('/mod_en');
const enUserNamespace = io.of('/user_en');
const enModNamespace = io.of('/mod_en'); // set auth
const leafids = [];
for (let i=7; i<332; i++) {
leafID = "path" + i.toString();
i += 3;
leafids.push(leafID);
const leafIds = [];
for (let i = 7; i < 332; i += 1) {
const leafId = `path${i.toString()}`;
i += 3;
leafIds.push(leafId);
}
var leavesdata = {}; //object of structure {socketid1: {leaftext:"text",leafid:"leafid"}, ...}
var leafno = 0;
var socketids = [];
user_en.on('connection', (socket) => {
let id = socket.id;
socket.emit('leaves data', leavesdata);
socket.on('leaf text', (leafdata) => {
console.log(leafdata);
if(!socketids.includes(id)) { //checks if socketid is already assigned with leafid
socketids.push(id);
leafdata["leafid"] = leafids[leafno]; //if not assign leafid
leafno = leafno+1;
leavesdata[id] = leafdata;
} else {
leavesdata[id]["leaftext"] = leafdata["leaftext"]; //if yes update the message
leafdata = leavesdata[id];
};
console.log(leafdata);
user_en.emit('leaf data', leafdata);
mod_en.emit('leaves data', leavesdata);
console.log(leavesdata);
fs.writeFileSync(filepath, JSON.stringify(leavesdata), function(err) {
if(err) return console.error(err);
});
});
const leavesData = {};
// object of structure {socketid1: {userName:"", leafText:"",leafId:""}, ...}
const notifyEveryone = function notifyUserStateChangeToEveryone() {
enUserNamespace.emit('leaves data', leavesData);
enModNamespace.emit('leaves data', leavesData);
};
const updateLeavesData = function udateLeavesDataAndNotify(socketId, leafData) {
leavesData[socketId] = { ...leavesData[socketId], ...leafData };
notifyEveryone();
};
enUserNamespace.on('connection', (socket) => {
const socketId = socket.id;
leavesData[socketId] = { userName: socket.handshake.query.userName }; // user
socket.emit('leaves data', leavesData);
socket.on('leaf text', (leafText) => {
const leafData = {};
leafData.leafText = leafText;
if (!(leavesData[socketId].leafText)) {
leafData.leafNo = leafIds.shift();
}
updateLeavesData(socketId, leafData);
// fs.writeFileSync(filepath, JSON.stringify(leavesData), (err) => {
// if (err) return console.error(err);
// });
});
});
mod_en.on('connection', (socket) => {
io.of('mod').emit('leaves data', leavesdata);
socket.on('message delete', id => {
console.log(leavesdata);
mod_en.emit('delete text', leavesdata[id]["leafid"]);
delete leavesdata[id];
const index = socketids.indexOf(id);
if (index > -1) {
socketids.splice(index, 1);
}
io.of('mod').emit('leaves data', leavesdata);
});
socket.on('save exit', filename => {
console.log("nsdiisd");
leavesdata = {};
leafno = 0;
socketids = [];
io.emit('leaves data', leavesdata);
io.of('mod').emit('leaves data', leavesdata);
enModNamespace.on('connection', (socket) => {
enModNamespace.emit('leaves data', leavesData);
socket.on('leaf delete', (socketId) => {
leafIds.push(leavesData[socketId].leafId);
delete leavesData[socketId].leafText;
delete leavesData[socketId].leafId;
notifyEveryone();
});
socket.on('leaf update', (leafData) => updateLeavesData(socket.id, leafData));
socket.on('user delete', (socketId) => {
leafIds.push(leavesData[socketId].leafId);
enUserNamespace.sockets.get(socketId).disconnect();
delete leavesData[socketId];
notifyEveryone();
});
socket.on('save exit', () => { // save data into file
enUserNamespace.disconnectSockets();
Object.keys(leavesData).forEach((key) => {
delete leavesData[key];
});
notifyEveryone();
});
});
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