forked from timjb/node-ardrone-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·43 lines (35 loc) · 1.07 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
#!/usr/bin/env node
var drone = require('ardrone')
, connect = require('connect')
, io = require('socket.io')
, fs = require('fs')
// Serve web interface
var server = connect.createServer(connect.static(__dirname + '/public'))
server.listen(8000)
// WebSocket
var socket = io.listen(server)
socket.on('connection', function(client) {
client.on('message', function(msg) {
if (msg.charAt(0) == '{') {
var props = JSON.parse(msg)
for (key in props) {
if (props.hasOwnProperty(key)) {
drone[key] = 0.4 * props[key]
}
}
} else {
var parts = msg.split(/\s/)
var cmd = parts[0]
var params = parts.slice(1)
for (var i = 0, l = params.length; i < l; i++) {
params[i] = parseFloat(params[i])
}
if (typeof drone[cmd] == 'function') {
console.log('Command sent: "' + cmd + '" With params: [' + params.join(', ') + ']')
drone[cmd].apply(drone, params)
} else {
console.log('NO SUCH COMMAND: ' + cmd)
}
}
})
})