-
Notifications
You must be signed in to change notification settings - Fork 2
/
LeapMotionDetect.js
82 lines (77 loc) · 2.2 KB
/
LeapMotionDetect.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
72
73
74
75
76
77
78
79
80
81
82
function LeapMotionDetect(callback, args, version) {
var portComm = 0;
var loop;
function loadLeapLib() {
loop = setInterval(checkLeapLib, 100);
newScript = document.createElement('script');
newScript.type = 'text/javascript';
if(version) {
newScript.src = '//js.leapmotion.com/leap-'+version+'.js';
} else {
newScript.src = '//js.leapmotion.com/leap-0.6.0.js';
}
document.getElementsByTagName('head')[0].appendChild(newScript);
}
function checkLeapLib() {
if (typeof Leap == 'object') {
console.log("LeapJS Library Version " + Leap.version.full + " Loaded!");
clearInterval(loop);
callback(true, args);
} else {
console.log("Waiting...");
}
}
setTimeout(function () {
if (portComm < 1) {
console.log("LEAP COMM PORT NOT DETECTED");
ws.close();
callback(false, args);
ws.onmessage = function () {};
}
if (portComm == 1) {
console.log("LEAP COMM PORT DETECTED BUT DEVICE IS NOT PROVIDING FRAME DATA WITHIN EXPECTED TIMEOUT THRESHOLD");
ws.close();
callback(false, args);
ws.onmessage = function () {};
}
}, 2000);
if ("WebSocket" in window) {
console.log("WebSocket is supported by your Browser!");
// Let us open a web socket
var ws = new WebSocket("ws://localhost:6437");
ws.onopen = function () {
// Web Socket is connected, send data using send()
ws.send("");
console.log("Detecting LEAP PORT...");
};
ws.onmessage = function (evt) {
var received_msg = evt.data;
console.log(evt);
console.log("Message is received..."+received_msg);
obj = JSON.parse(received_msg);
if (obj.version && portComm == 0) {
portComm++;
console.log("LEAP PORT DETECTED");
} else if (obj.currentFrameRate && portComm > 0) {
portComm++;
ws.close();
console.log("Load the full Leap Library!");
ws.onmessage = function () {};
loadLeapLib();
} else {
console.log("LEAP NOT DETECTED");
ws.close();
callback(false, args);
ws.onmessage = function () {};
}
};
ws.onclose = function () {
// websocket is closed.
console.log("Connection is closed...");
};
} else {
// The browser doesn't support WebSocket
console.log("WebSocket NOT supported by your Browser!");
callback(null, args);
}
}