This repository has been archived by the owner on Jul 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
78 lines (60 loc) · 1.59 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
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
const redis = require("redis");
app.use(bodyParser.urlencoded({
extended: true
}));
let client = redis.createClient();
client.on("connect", function() {
console.log("redis connected");
});
client.flushall();
app.use(express.static("public"));
const io = require("socket.io")(8080);
let users = [];
client.set("HandsRaised",0);
io.on("connection", socket => {
socket.emit("pageOpened",users[0]);
socket.on("raise-hand", function() {
client.incr("HandsRaised");
status();
});
socket.on("hand-raised", function() {
client.decr("HandsRaised");
status();
});
});
var cookieParser = require('cookie-parser');
var session = require('express-session')
app.use(cookieParser());
app.use(session({
secret: '34SDgsdgspxxxxxxxdfsG', // just a long random string
resave: false,
saveUninitialized: true
}));
app.listen(3000,function(){
console.log("Server started at port 3000");
});
app.get("/", function(req, res) {
const id = req.sessionID;
res.render("home.ejs");
if (users.indexOf(id) == -1) {
users.unshift(id);
client.lpush(["UsersId",id]);
console.log("new user");
} else {
console.log("somebody returned");
}
status();
});
app.get("/chatroom",function(req,res){
res.render("chatroom.ejs");
});
function status() {
client.get("HandsRaised",function(err,hands){
client.lrange("UsersId",0,-1,function(err,usersList){
console.log("Active Users = "+ usersList.length+ " Hands Raised = "+ (hands/usersList.length)*100+"%");
});
});
};