-
Notifications
You must be signed in to change notification settings - Fork 4
/
app.js
175 lines (143 loc) · 4.84 KB
/
app.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
const http= require("http");
const express = require("express");
const path=require("path");
const socketio = require("socket.io");
const keys = require("./config/keys");
const cookieParser = require("cookie-parser");
const cookieSession = require("cookie-session");
const bodyParser = require("body-parser");
const methodOverride = require('method-override');
require("./db/mongoose");
const student = require("./routes/api/student");
const teacher = require("./routes/api/teacher");
const superuser = require("./routes/api/superuser");
const authRoutes = require("./routes/api/auth");
const passport = require("passport");
const cron = require("node-cron");
const Discussion = require("./models/Discussion");
const SuperUser = require("./models/SuperUser");
const Teacher = require("./models/Teacher");
const Student = require("./models/Student");
const app=express();
const PORT = process.env.PORT || 3000;
const server=http.createServer(app)
const io = socketio(server);
app.use(express.json());
app.set("view engine", "ejs");
app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.use(express.static("./assets"));
app.use(
cookieSession({
name: "session",
keys: [keys.sessionSecret]
})
);
app.locals.moment = require("moment");
app.use(cookieParser());
require("./middleware/PassportMiddleware");
app.use(passport.initialize());
app.use(passport.session());
app.get("/",(req,res)=>{
if (req.session.token == null) {
res.render("home", {
currentUser: req.user,
clientType: req.session.client
});
}else{
SuperUser.findOne({email:req.user.email},(err,superuser)=>{
if(err) Error(err);
if(superuser){
res.redirect("/superuser/dashboard");
}else{
Teacher.findOne({ email: req.user.email }, function (err, teacher) {
if (err) Error(err);
if (teacher) {
res.redirect("/teacher/dashboard");
} else {
res.redirect("/student/dashboard");
}
});
}
})
}
});
app.use("/student", student);
app.use("/teacher", teacher);
app.use("/superuser", superuser);
app.use("/auth", authRoutes);
io.on('connection',(socket)=>{
console.log("New Web Socket Connection");
socket.on('join',({useremail,room},callback)=>{
socket.join(room);
Discussion.findOne({roomId:room},async (err,discussion)=>{
discussion.socketids.push(socket.id);
await discussion.save();
});
});
socket.on('sendMessage',(msg,callback)=>{
msg.createdAt=new Date().getTime();
Discussion.findOne({roomId:msg.roomId},async (err,discussion)=>{
discussion.texts.push(msg);
await discussion.save();
discussion.socketids.forEach((id)=>{
io.to(id).emit('message',msg);
});
});
callback();
});
socket.on('disconnect',()=>{
Discussion.find({},async (err,discussions)=>{
discussions.forEach(async (discussion)=>{
if(discussion.socketids.indexOf(socket.id)!==-1){
const pos = discussion.socketids.indexOf(socket.id);
discussion.socketids.splice(pos,1);
await discussion.save();
}
});
});
console.log("Disconnected");
});
socket.on('join-room',(roomId,userId)=>{
socket.join(roomId);
socket.to(roomId).broadcast.emit("user-connected",userId);
socket.on('message',message=>{
Discussion.findOne({roomId:message.room},async (err,discussion)=>{
let y= discussion.students.findIndex(x=>x.email === message.msg);
if(discussion.email === message.user){
io.to(roomId).emit('createMessage',{
user:discussion.admin,
msg:message.msg
});
}else if(y===-1){
Student.findOne({email:message.user},(err,stud)=>{
io.to(roomId).emit('createMessage',{
user:stud.name,
msg:message.msg
});
});
}else{
discussion.students[y].present=true;
await discussion.save();
Student.findOne({email:message.user},(err,stud)=>{
io.to(roomId).emit('createMessage',{
user:stud.name,
msg:message.msg
});
});
}
});
});
});
});
// var ExpressPeerServer = require('peer').ExpressPeerServer;
// var peerExpress = require('express');
// var peerApp = peerExpress();
// var peerServer = require('http').Server(peerApp);
// var options = { debug: true }
// var peerPort = 443;
// peerApp.use('/peerjs', ExpressPeerServer(peerServer, options));
server.listen(PORT,()=>{
console.log("Server Started ");
});
// peerServer.listen(peerPort)