Spaces:
Running
Running
File size: 4,638 Bytes
df72131 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
const express = require("express");
const { WebSocketServer } = require("ws");
const cookieParser = require("cookie-parser");
const db = require("./config/DatabaseConfig.js");
const app = express();
const path = require("path");
const http = require("http");
var https = require("https");
const socketIO = require("socket.io");
const server = http.createServer(app);
const io = socketIO(server);
const { stat } = require("fs");
const cors = require("cors");
const fs = require("fs");
const util = require("util");
const WebSocket = require("ws");
const fetch_user = require("./helpers/SessionIdhelper.js");
const { userOnline } = require("./sockets/TeamConnectionStatus.js");
const notification = require("./sockets/Notification.js");
const round = require("./sockets/RoundStartEnd.js");
const {
scheduleRound1Start,
scheduleRound1End,
} = require("./timers/RoundOneTimer.js");
const {
scheduleRound2Start,
scheduleRound2End,
} = require("./timers/RoundTwoTimer.js");
const {
scheduleRound3Start,
scheduleRound3End,
} = require("./timers/RoundThreeTimer.js");
const {
scheduleRound4Start,
scheduleRound4End,
} = require("./timers/RoundFourTimer.js");
// const logFile = fs.createWriteStream(path.join(__dirname, '/logs/logs.txt'), { flags: 'a' });
// const originalConsoleLog = console.log;
// console.log = function (...args) {
// const timestamp = new Date().toISOString();
// const message = args.map(arg => util.format(arg)).join(' ');
// const logMessage = `${timestamp} - ${message}`;
// logFile.write(logMessage + '\n');
// process.stdout.write(logMessage + '\n');
// originalConsoleLog.apply(console, args);
// };
const corsOptions = {
origin: [
"http://localhost:3000",
"http://127.0.0.1:3000",
"http://192.168.1.6:3000",
// your origins here
],
credentials: true,
exposedHeaders: ["set-cookie"],
};
app.use(cookieParser());
app.use(cors(corsOptions));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static(path.join(__dirname, "/logs")));
app.get("/", (req, res) => {
res.send("Server is Up and Running");
console.log("Somebody accessed the server broo");
});
app.use(express.static(path.join(__dirname, "/static")));
app.set("socketio", io);
io.sockets.setMaxListeners(500);
start_routes();
admin_routes();
start_timers();
server.listen(process.env.PORT, "0.0.0.0", () => {
console.log("Server running");
const currentDate = new Date();
console.log(currentDate);
});
function start_routes() {
app.use("/register", require("./routers/UserRegisterRouter.js"));
app.use("/login", require("./routers/UserLoginRouter.js"));
app.use("/question", require("./routers/QuestionRouter.js"));
app.use("/round", require("./routers/RoundsRouter.js"));
app.use("/model", require("./routers/ModelRouter.js"));
app.use("/verify", require("./routers/VerifyRouter.js"));
}
function admin_routes() {
app.use("/qsns", require("./Admin/Routers/QsEnterRouter.js"));
app.use("/rounds", require("./Admin/Routers/RoundEnterRouter.js"));
app.use("/room", require("./Admin/Routers/RoomEnterRouter.js"));
}
async function start_timers() {
const {
scheduleCollaborativeRound,
} = require("./timers/RoundThreeCollaborator.js");
const Round = require("./models/RoundsModel.js");
let Round1 = await Round.findOne({ Round: 1 });
let Round2 = await Round.findOne({ Round: 2 });
let Round3 = await Round.findOne({ Round: 3 });
let Round4 = await Round.findOne({ Round: 4 });
let Round1STime = Round1.StartTime;
let Round2STime = Round2.StartTime;
let Round3STime = Round3.StartTime;
let Round4STime = Round4.StartTime;
let Round1ETime = Round1.EndTime;
let Round2ETime = Round2.EndTime;
let Round3ETime = Round3.EndTime;
let Round4ETime = Round4.EndTime;
let date = new Date();
//scheduleCollaborativeRound(date)
scheduleRound1Start(Round1STime);
scheduleRound2Start(Round2STime);
scheduleRound3Start(Round3STime);
scheduleRound4Start(Round4STime);
scheduleRound1End(Round1ETime);
scheduleRound2End(Round2ETime);
scheduleRound3End(Round3ETime);
scheduleRound4End(Round4ETime);
}
app.use((req, res, next) => {
res.status(404).sendFile(path.join(__dirname, "static/html/404.html"));
});
notification.initialize(io);
round.initialize(io);
io.on("connection", async (socket) => {
const cookies = socket.request.headers.cookie;
let user = await fetch_user(cookies);
if (!user) {
return;
}
if (socket.rooms.length !== 2) socket.join(user.TeamID.toString());
io.to(user.TeamID.toString()).emit(
"notofication",
"user is online " + user.Name
);
});
|