-
Notifications
You must be signed in to change notification settings - Fork 126
/
Copy pathdroneConnection.js
54 lines (41 loc) · 1.21 KB
/
droneConnection.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
var droneConnection = (function() {
var my = {};
var droneAddress = "ws://127.0.0.1:8001/";
var websocket;
var tracker;
var updatingElement;
function droneWebSocket(url) {
websocket = new WebSocket(url);
websocket.onopen = onOpen;
websocket.onclose = onClose;
websocket.onerror = onError;
websocket.onmessage = onMessage;
}
function onOpen() {
console.log("Connection open!");
}
function onClose() {
console.log("Connection closed");
}
function onError() {
console.log("Connection error:");
}
function onMessage(message) {
console.log(message);
// Received a new snapshot form the drone camera
if (message.data instanceof Blob) {
// console.log("received message");
TrackerUtils.loadImage(message.data, updatingElement);
tracking.track(updatingElement, tracker);
}
}
my.streamImage = function(colorTracker, element) {
droneWebSocket(droneAddress);
tracker = colorTracker;
updatingElement = element;
}
my.send = function(move) {
websocket.send(JSON.stringify(move))
}
return my;
}());