Select Git revision
-
aidanjakes authoredaidanjakes authored
app.js 4.64 KiB
'use strict';
//azure app key
const APP_KEY="jwJMNoxZnb/35swfpI2oSt89CAN9P8W2723yZT7S2vKvvfF7dovajQ==";
//Set up express
const express = require('express');
const app = express();
//Setup socket.io
const server = require('http').Server(app);
const io = require('socket.io')(server);
const got = require('got');
const request = require('request');
const moment = require('moment');
//Setup static page handling
app.set('view engine', 'ejs');
app.use('/static', express.static('public'));
//Handle client interface on /
app.get('/', (req, res) => {
res.render('client');
});
//Handle display interface on /display
app.get('/display', (req, res) => {
res.render('display');
});
//Start the server
function startServer() {
const PORT = process.env.PORT || 8080;
server.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
}
let chatMembers = []; //list of players
let membersToSockets = new Map(); //player number to socket
let socketsToMembers = new Map();
let chatIDs = [];
function handleLogin(username,socket){
//let everyone know someone joined
const un = username.username;
if(chatMembers.includes(un)) return;
chatMembers.push(un);
membersToSockets.set(un,socket);
socketsToMembers.set(socket,un);
console.log(un);
//let them know login has been successful
socket.emit('loginSuccess',true);
//tell everyone a new player has joined
updateAll();
//get the chats they're in
request('http://localhost:7071/api/HttpGetChatsForUserID', {
json: true,
body: {
memberID: un
}
}, function (error, response, body){
chatIDs = body;
//upon receiving chatIDs, get most recent message of each one
getMostRecentMessages(chatIDs,socket);
});
/*
//send them all the messages from the chat
sendAllChatMessages(socket);
*/
}
function getMostRecentMessages(chatIds,socket){
//go through each chat, get most recent message and pass this information to the client
for (const chatID of chatIDs){
request('http://localhost:7071/api/HttpGetLastChatMessage', {
json: true,
body: {
"ChatID": chatID
}
}, function (error, response, body){
console.log(body);
//upon receiving it, tell the client to store the chatID
if(body != "error"){
body['ChatID'] = chatID
socket.emit('chatAndLastMsg',body);
}
});
}
}
function sendAllChatMessages(chatID,socket){
request('http://localhost:7071/api/HttpRetrieveAllChatMessages', {
json: true,
body: {
ChatID: chatID
}
}, function (error, response, body){
const new_dict = {ChatID: chatID, messages: body}
socket.emit('allChatMessages',new_dict);
});
}
//when you receive a message, tell everyone the date and time it was sent
function handleChatReceived(userAndMsgAndChatID){
//when server receives a message, send it to everyone
console.log(userAndMsgAndChatID.username + " " + userAndMsgAndChatID.message);
var now = new moment();
const date = now.format("YYYY-MM-DD");
const time = now.format("HH:mm:ss");
console.log(date + " " + time);
const userMsgDatetimeChat = {username: userAndMsgAndChatID.username, message: userAndMsgAndChatID.message,
ChatID: userAndMsgAndChatID.ChatID,date: date, time: time,};
io.emit('newMessage',userMsgDatetimeChat);
//then store message in database -
(async () => {
const body = await got.post('http://localhost:7071/api/HttpStoreChatMessage', {
json: {
username: userAndMsgAndChatID.username,
message: userAndMsgAndChatID.message,
ChatID: userAndMsgAndChatID.ChatID,
date: date,
time: time
}
}).json();
console.log(body.msg);
if(body.msg == "OK"){
console.log('message stored');
} else {
console.log("error storing message");
}
})();
}
//tell everyone a new player has joined
function updateAll(){
for(let [member,socket] of membersToSockets){
socket.emit('updateMembers',chatMembers);
}
}
//Handle new connection
io.on('connection', socket => {
console.log('New connection');
socket.on('login',username => {
handleLogin(username,socket);
});
socket.on('sendChat',userAndMsg => {
console.log('sendChat received');
handleChatReceived(userAndMsg);
});
socket.on('requestChatMessages',ChatID =>{
//send messages from corresponding chatID
sendAllChatMessages(ChatID,socket);
console.log('requestChatMessages ' + ChatID);
})
//Handle disconnection
socket.on('disconnect', () => {
console.log('Dropped connection');
});
});
//Start server
if (module === require.main) {
startServer();
}
module.exports = server;