-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
executable file
·62 lines (42 loc) · 1.21 KB
/
server.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
var express = require('express');
var http = require('http');
var WebSocket = require('ws')
//import modules
var robota = require('./robota/robotaAsync');
var router = express();
var server = http.createServer(router);
const ws = new WebSocket.Server({ server });
router.use(requireHTTPS);
router.use(express.static(__dirname + "/public"));
router.use(express.bodyParser());
function requireHTTPS(req, res, next) {
if (req.headers.host != 'localhost:3000') {
if (req.headers["x-forwarded-proto"] === "https") {
// OK, continue
return next();
};
res.redirect('https://' + req.headers.host + req.url);
}
else {
return next();
}
}
//client websocket
ws.on('connection', function connection(ws) {
console.log('client connected')
ws.on('error', function error(err) {
console.log('Error: ' + err.code);
});
robota.openClientSocket(ws)
});
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function () {
console.log("Server listening");
robota.start();
//robota.openClientSocket(ws)
});
router.use(function (req, res) {
res.sendfile(__dirname + '/public/index.html');
});
process.on('uncaughtException', function (err) {
//console.log(err);
});