-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
135 lines (117 loc) · 3.27 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
/**
* Module dependencies.
*/
var express = require('express');
var path = require('path');
var session = require("express-session");
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var store = new session.MemoryStore();
var app = express();
var httpServer = require("http").Server(app);
var WebSocketServer = require("websocket").server;
var webSocketServer = new WebSocketServer({
httpServer:httpServer,
autoAcceptConnections:false,
maxReceivedFrameSize: 64*1024*1024,
maxReceivedMessageSize: 64*1024*1024,
});
var signKey = "RHDwSKMa&vcxnkj";
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(cookieParser(signKey));
app.use(session({store:store}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
app.use(express.static(path.join(__dirname, 'public')));
var sockets = [];
var camsocket;
webSocketServer.on('request', function(request) {
var sid = "";
for (var i = 0; i < request.cookies.length; i++) {
if(request.cookies[i].name === "connect.sid"){
sid = request.cookies[i].value;
}
};
var sessionID = cookieParser.signedCookie(sid,signKey);
store.get(sessionID, function(err,session){
//WebSocket Endpoint fuer den Beobachter
if(request.resource == "/watch"){
var connection = request.accept();
sockets.push(connection);
connection.on("close", function(){
delete connection;
});
}
//WebSocket Endpoint fuer die Kamera
else if(request.resource=="/cam"){
//Schliessen der WebSocket-Verbindung, einer evtl. bereits verbundenen Kamera
if(camsocket){
camsocket.close();
}
camsocket = request.accept();
camsocket.on("message", function(message){
if(message.type=="binary"){
for(var i = 0 ; i< sockets.length;i++){
sockets[i].sendBytes(message.binaryData);
}
}
});
camsocket.on("close",function(){
for(var i = 0 ; i< sockets.length;i++){
sockets[i].sendUTF("Webcam closed");
}
});
}
else{
request.reject();
}
});
});
function requiresLogin(req,res,next){
if(!req.session.username){
req.session.path = req.url;
res.redirect('/login');
}
else{
next();
}
}
app.get('/login', function (req,res){
res.render('login',{title:'Login'});
});
app.post('/login',function(req,res){
if(req.body.username=="admin"&&req.body.password=="123"){
var path = req.session.path;
req.session.regenerate(function(err){
var hour = 60*60*1000;
req.session.username = req.body.username;
req.session.cookie.expires = new Date(Date.now()+hour);
if(path){
res.redirect(path);
}
else{
res.redirect("/watch");
}
});
}
else{
res.redirect('back');
}
});
app.get("/",function(req,res){
res.redirect("/login");
});
app.get('/cam',requiresLogin,function (req,res){
res.sendFile(__dirname+"/static/cam.html")
});
app.get("/watch",requiresLogin,function (req,res){
res.sendFile(__dirname+"/static/watch.html")
});
app.get("/logout",function (req,res){
req.session.destroy();
res.redirect("/login");
});
httpServer.listen(3000, function (){
console.log("Express-Server laeuft auf dem Port 3000");
});