Select Git revision
index.js
Markus Klose authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
index.js 6.72 KiB
//node packages
const express = require('express');
const app = express();
const http = require('http');
const { mongo, default: mongoose } = require('mongoose');
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
const bodyParser = require('body-parser');
const registerRoute = require('./routes/register');
const loginRoute = require('./routes/login');
const fs = require('fs');
const activeUsers = new Map();
const authenticationRoute = require('./routes/auth');
const { response } = require('express');
const { resolve } = require('path');
//DB Connection
mongoose.connect("mongodb://127.0.0.1:27017/cloudComputing");
app.use(bodyParser.json());
//Routes
app.use('/register', registerRoute);
app.use('/login', loginRoute);
app.use('/authenticate', authenticationRoute)
//Views
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/start.html');
});
app.get('/chatroom', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
app.get('/register', (req, res) => {
res.sendFile(__dirname + '/views/register.html');
});
app.get('/login', (req, res) => {
res.sendFile(__dirname + '/views/login.html');
});
io.on('connection', (socket) => {
// Triggered by emit() from client, when entering the chatroom
// SocketID => name
socket.on('userLogin', async function(response) {
await addUserToActiveUsers(response, socket);
});
// when disconnecting from chatroom
socket.on('disconnect', async function(response) {
await deleteUserFromActiveUsers(response, socket);
});
});
/**
* Summary:
* Function to add a user to map of users, who are currently online.
*
* Description:
* Function will take the username from response and append it together with the socketID to map activeUsers.
* after appending, ip.emit() will send the updated userlist to all users who are currently online.
*
* @param {socket} socket
* @param {Object} response
* @triggers io.emit() 'updateUserlist' event
* @listens socket.on() 'userLogin' event
*/
async function addUserToActiveUsers(response, socket) {
return new Promise(resolve => {
activeUsers.set(socket.id, response.name);
var userArr = Array.from(activeUsers.values());
io.emit('updateUserlist', {
userList : userArr
});
});
}
/**
* Summary:
* Function to delete a user from the map of users, who are currently online.
*
* Description:
* Function will take the socketID from the socket, of the client, who is disconneting.
* The socketID and its value (username) will be deleted from map activeUsers.
* After deleting the socketID and username io.emit() is called and the upodated userList is sent to all users who are currently online.
*
* @param {socket} socket
* @param {Object} response
* @triggers io.emit() 'updateUserlist' event
* @listens socket.on() 'disconnect' event
*/
async function deleteUserFromActiveUsers(response, socket) {
return new Promise(resolve => {
activeUsers.delete(socket.id);
var userArr = Array.from(activeUsers.values());
io.emit('updateUserlist', {
userList : userArr
});
});
}
/**
* Summary:
* Function for forwarding chat messages.
*
* Description:
* Function will take the username of the sender and the message from the response.
* Server will append the current time and call io.emit() 'chat message' event.
* Json with the message, username of sender and current time is sent to all users who are currently online.
*
*
* @param {Object} response
* @triggers io.emit() 'chat message' event
* @listens socket.on() 'chat message' event
*/
io.on('connection', (socket) => {
socket.on('chat message', (response) =>{
var msg = response.msg;
var name = response.name;
var dateTime = new Date().toTimeString();
io.emit('chat message', {
msg : msg,
name : name,
dateTime: dateTime
});
})
});
/**
* Summary:
* Function for forwarding private chat messages.
*
* Description:
* Function will take the username of the sender, the private message, the name of the receiver from the response.
* Server will append the current time and call io.emit() 'chat message' event.
*
*
*
* @param {Object} response
* @triggers io.emit() 'chat message' event
* @listens socket.on() 'chat message' event
*/
io.on('connection', (socket) => {
socket.on('private message', (response) => {
var private_msg = response.msg;
var receiver = response.receiver;
var sender = response.name;
var array = Array.from(activeUsers);
var dateTime = new Date().toTimeString();
for(var i = 0; i<array.length; i++) {
if(array[i][1] == receiver){
io.emit(receiver, {
msg : private_msg,
sender : sender,
receiver: receiver,
dateTime: dateTime
})
io.emit(sender, {
msg : private_msg,
sender : sender,
receiver: receiver,
dateTime: dateTime
})
return 0;
}
}
io.emit(sender, {
status: 401
})
})
socket.on('groupMessage', (response) => {
let sender = response.name;
let receivers = response.receivers;
let groupMsg = response.groupMsg;
var dateTime = new Date().toTimeString();
console.log("receivers", receivers[0]);
console.log("groupmsg", groupMsg);
let a = activeUsers.values;
console.log("sender", sender);
for (let i = 0; i<receivers.length; i++) {
io.emit(receivers[i].trim(), {
msg: groupMsg,
sender: sender,
receiver: receivers,
dateTime: dateTime
})
}
io.emit(sender, {
msg: groupMsg,
sender: sender,
receiver: receivers,
dateTime: dateTime
})
})
})
async function getKeyFromVal(receiver){
return new Promise( () => {
var array = Array.from(activeUsers);
console.log("usersArray: " + array);
var receiverSocketId;
for (var i = 0; i<array.length; i++){
console.log("array[i] ", array[i][0]);
if(array[i][1] == receiver) {
receiverSocketId = array[i][0];
resolve(receiverSocketId);
} else {
resolve("false");
}
}
})
}
//Logs message from Socket to Console
io.on('connection', (socket) => {
socket.on('chat message', (response) => {
var msg = response.msg;
});
});
//Send Image File
io.on("connection", (socket) => {
socket.on('sendImage', (file, callback) => {
io.emit('image', file);
});
});
//Send Video File
io.on("connection", (socket) => {
socket.on('sendVideo', (file, callback) => {
io.emit('video', file);
});
});
//Send Audio File
io.on("connection", (socket) => {
socket.on('sendAudio', (file, callback) => {
io.emit('audio', file);
});
});
server.listen(3000, () => {
console.log('listening on Port :3000');
});
module.exports = {activeUsers};