forked from muaddib1984/gr-webspectrum
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
48 lines (43 loc) · 1.16 KB
/
script.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
'use strict';
function connectWebSocket(spectrum) {
var ws = new WebSocket("ws://" + window.location.host + "/websocket");
ws.onopen = function(evt) {
console.log("connected!");
}
ws.onclose = function(evt) {
console.log("closed");
setTimeout(function() {
connectWebSocket(spectrum);
}, 1000);
}
ws.onerror = function(evt) {
console.log("error: " + evt.message);
}
ws.onmessage = function (evt) {
var data = JSON.parse(evt.data);
if (data.s) {
spectrum.addData(data.s);
} else {
if (data.center) {
spectrum.setCenterHz(data.center);
}
if (data.span) {
spectrum.setSpanHz(data.span);
}
}
}
}
function main() {
// Create spectrum object on canvas with ID "waterfall"
var spectrum = new Spectrum(
"waterfall", {
spectrumPercent: 20
});
// Connect to websocket
connectWebSocket(spectrum);
// Bind keypress handler
window.addEventListener("keydown", function (e) {
spectrum.onKeypress(e);
});
}
window.onload = main;