-
Notifications
You must be signed in to change notification settings - Fork 52
/
server.js
60 lines (60 loc) · 2.03 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
57
58
59
60
(function() {
var app, currentImg, drone, express, faye, imageSendingPaused, path, server, socket;
express = require("express");
faye = require("faye");
path = require("path");
drone = require("ar-drone").createClient();
drone.config('general:navdata_demo', 'TRUE');
app = express();
app.configure(function() {
app.set('port', process.env.PORT || 3001);
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
return app.use("/components", express.static(path.join(__dirname, 'components')));
});
server = require("http").createServer(app);
new faye.NodeAdapter({
mount: '/faye',
timeout: 45
}).attach(server);
socket = new faye.Client("http://localhost:" + (app.get("port")) + "/faye");
socket.subscribe("/drone/move", function(cmd) {
var _name;
console.log("move", cmd);
return typeof drone[_name = cmd.action] === "function" ? drone[_name](cmd.speed) : void 0;
});
socket.subscribe("/drone/animate", function(cmd) {
console.log('animate', cmd);
return drone.animate(cmd.action, cmd.duration);
});
socket.subscribe("/drone/drone", function(cmd) {
var _name;
console.log('drone command: ', cmd);
return typeof drone[_name = cmd.action] === "function" ? drone[_name]() : void 0;
});
server.listen(app.get("port"), function() {
return console.log("Express server listening on port " + app.get("port"));
});
currentImg = null;
drone.on('navdata', function(data) {
return socket.publish("/drone/navdata", data);
});
imageSendingPaused = false;
drone.createPngStream().on("data", function(frame) {
currentImg = frame;
if (imageSendingPaused) {
return;
}
socket.publish("/drone/image", "/image/" + (Math.random()));
imageSendingPaused = true;
return setTimeout((function() {
return imageSendingPaused = false;
}), 100);
});
app.get("/image/:id", function(req, res) {
res.writeHead(200, {
"Content-Type": "image/png"
});
return res.end(currentImg, "binary");
});
}).call(this);