This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
71 lines (53 loc) · 1.77 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
var fs = require('fs');
var express = require('express');
var app = express();
var https = require('https');
var Sensors = require('./sensors.js');
var io = require('socket.io');
//var http = require('http').Server( app );
var httpsOptions = {
key : fs.readFileSync("./server.key"),
cert : fs.readFileSync("./server.crt"),
requestCert : true,
rejetUnauthorized : false
};
var sensors = new Sensors();
var httpsServer = https.createServer(httpsOptions, app).listen(1337, function () {
console.log('Controller + visualizer app listening on port 1337!')
setInterval(function() {
sensors.orientation.alpha += Math.random() - .5;
sensors.orientation.beta += Math.random() - .5;
sensors.orientation.gamma += Math.random() - .5;
sensors.height = (1+Math.cos( Date.now() / 1000 - Math.PI/2) );
}, 10);
ready();
});
io = io.listen( httpsServer );
//var httpServer = express.createServer().get('*', (req, res)=>{ res.redirect("https://localhost:1337"+req.url); }).listen(1337);
app.use(express.static('./public/'));
app.get("/sensors", function( req, res ) {
res.json(sensors.getData());
});
io.on( 'connection', function( socket ) {
console.log('a user '+ socket.id +' connected');
socket.on( 'disconnect', function() {
console.log('user '+ socket.id +' disconnected');
});
socket.on( "pling", function( msg ) {
console.log('user '+ socket.id +' pling\'d', arguments);
socket.emit( 'plong', msg );
});
socket.on( 'ctrl', function( json ) {
json = JSON.parse( json );
if (!!json.longitudinal) {
sensors.move(json.longitudinal);
}
console.info("ctrl from user "+ socket.id, arguments)
});
});
function ready() {
ready = function(){};
setInterval( function() {
io.emit("sensors", sensors.getData() );
}, 10);
}