Commit 79a85b18 authored by Kannan V M's avatar Kannan V M
Browse files

Add multi room support

Currently supports en, ml and hi
parent 6789e0db
......@@ -21,16 +21,16 @@ server.listen(port, () => {
// assign static path to public folder
app.use(express.static(path.join(__dirname, '/public/')));
app.get('/mod_en', (req, res) => {
app.get(['/mod_en', '/mod_ml', '/mod_hi'], (req, res) => {
res.sendFile(path.join(__dirname, '/public/mod.html'));
});
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, '/index.html'));
app.get(['/en', '/ml', '/hi'], (req, res) => {
res.sendFile(path.join(__dirname, '/public/index.html'));
});
const enUserNamespace = io.of('/user_en');
const enModNamespace = io.of('/mod_en'); // set auth
const userNamespace = io.of('/user');
const modNamespace = io.of('/mod'); // set auth
const leafIds = [];
for (let i = 7; i < 332; i += 1) {
......@@ -39,68 +39,91 @@ for (let i = 7; i < 332; i += 1) {
leafIds.push(leafId);
}
const leavesData = {};
const roomLeafIds = {
en: { leafIds: [...leafIds] },
ml: { leafIds: [...leafIds] },
hi: { leafIds: [...leafIds] },
};
const leavesData = { en: {}, ml: {}, hi: {} };
// object of structure {socketid1: {userName:"", leafText:"",leafId:""}, ...}
const notifyEveryone = function notifyUserStateChangeToEveryone() {
enUserNamespace.emit('leaves data', leavesData);
enModNamespace.emit('leaves data', leavesData);
const notifyEveryone = function notifyUserStateChangeToEveryone(roomId) {
userNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
modNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
};
const updateLeavesData = function udateLeavesDataAndNotify(socketId, leafData) {
if (!(leavesData[socketId].leafId)) {
leavesData[socketId].leafId = leafIds.shift();
const updateLeavesData = function updateLeavesDataAndNotify(socketId, roomId, leafData) {
if (!(leavesData[roomId][socketId].leafId)) {
leavesData[roomId][socketId].leafId = roomLeafIds[roomId].leafIds.shift();
}
leavesData[socketId] = { ...leavesData[socketId], ...leafData };
notifyEveryone();
leavesData[roomId][socketId] = { ...leavesData[roomId][socketId], ...leafData };
notifyEveryone(roomId);
};
const deleteLeaf = function deleteLeafAndNotifyEveryone(socketId) {
const { leafId } = leavesData[socketId];
delete leavesData[socketId].leafText;
delete leavesData[socketId].leafId;
leafIds.push(leafId);
enUserNamespace.emit('leaf delete', leafId);
enModNamespace.emit('leaves data', leavesData);
const deleteLeaf = function deleteLeafAndNotifyEveryone(socketId, roomId) {
const { leafId } = leavesData[roomId][socketId];
delete leavesData[roomId][socketId].leafText;
delete leavesData[roomId][socketId].leafId;
roomLeafIds[roomId].leafIds.push(leafId);
userNamespace.to(roomId).emit('leaf delete', leafId);
modNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
};
enUserNamespace.on('connection', (socket) => {
userNamespace.on('connection', (socket) => {
const socketId = socket.id;
leavesData[socketId] = { userName: socket.handshake.query.userName }; // user
socket.emit('leaves data', leavesData);
const { userName, roomId } = socket.handshake.query;
if (roomId === 'en' || roomId === 'ml' || roomId === 'hi') {
socket.join(roomId);
socket.on('leaf text', (leafText) => {
const leafData = {};
leafData.leafText = leafText;
if (!(leavesData[socketId].leafText)) {
leafData.leafId = leafIds.shift();
}
leavesData[roomId][socketId] = { userName }; // user
socket.emit('leaves data', leavesData[roomId]);
updateLeavesData(socketId, leafData);
socket.on('leaf text', (leafText) => {
const leafData = {};
leafData.leafText = leafText;
if (!(leavesData[roomId][socketId].leafText)) {
leafData.leafId = roomLeafIds[roomId].leafIds.shift();
}
// fs.writeFileSync(filepath, JSON.stringify(leavesData), (err) => {
updateLeavesData(socketId, roomId, leafData);
// fs.writeFileSync(filepath, JSON.stringify(leavesData[roomId]), (err) => {
// if (err) return console.error(err);
// });
});
});
} else {
socket.disconnect();
}
});
enModNamespace.on('connection', (socket) => {
enModNamespace.emit('leaves data', leavesData);
socket.on('leaf delete', (socketId) => {
deleteLeaf(socketId);
});
socket.on('leaf update', (data) => updateLeavesData(data.socketId, data.leafData));
socket.on('user delete', (socketId) => {
deleteLeaf(socketId);
enUserNamespace.sockets.get(socketId).disconnect();
delete leavesData[socketId];
enModNamespace.emit('leaves data', leavesData);
});
socket.on('save exit', () => { // save data into file
enUserNamespace.disconnectSockets();
Object.keys(leavesData).forEach((key) => {
delete leavesData[key];
modNamespace.on('connection', (socket) => {
const { roomId } = socket.handshake.query;
if (roomId === 'en' || roomId === 'ml' || roomId === 'hi') {
socket.join(roomId);
modNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
socket.on('leaf delete', (socketId) => {
deleteLeaf(socketId, roomId);
});
socket.on('leaf update', (data) => {
updateLeavesData(data.socketId, roomId, data.leafData);
});
socket.on('user delete', (socketId) => {
deleteLeaf(socketId, roomId);
userNamespace.sockets.get(socketId).disconnect();
delete leavesData[roomId][socketId];
modNamespace.to(roomId).emit('leaves data', leavesData[roomId]);
});
notifyEveryone();
});
socket.on('save exit', () => { // save data into file
userNamespace.in(roomId).disconnectSockets();
Object.keys(leavesData[roomId]).forEach((key) => {
delete leavesData[roomId][key];
});
roomLeafIds[roomId].leafIds = leafIds;
notifyEveryone(roomId);
});
} else {
socket.disconnect();
}
});
const mod = io.connect('/mod_en');
const ROOM = window.location.href.split('_')[1];
const mod = io.connect('/mod',{
query: {
roomId: ROOM,
}
});
const tBody = document.querySelector('tbody');
......
/* @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=
* agpl-3.0.txt AGPL-3.0-or-later */
const socket = io('/user_en');
const ROOM = window.location.href.split('/')[3];
const socket = io('/user', {
query: {
roomId: ROOM,
}
});
function popupLeaf() {
let knowledgeId = this.id.replace('path', 'knowledge');
......
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