-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient.js
More file actions
43 lines (37 loc) · 1.36 KB
/
Copy pathclient.js
File metadata and controls
43 lines (37 loc) · 1.36 KB
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
var ws = new WebSocket('ws://127.0.0.1:8000');
//var ws = new WebSocket('ws://sparber.net:62249');
ws.onopen = function (event) {
//ws.send(JSON.stringify({call:"busstopRequest", query:"stazione"}));
document.getElementById("input").oninput = function () {
var query = document.getElementById("input").value;
console.log(query);
ws.send(JSON.stringify({call:"busstopRequest", "query":query}));
}
};
ws.onmessage = function(data, flags) {
data = JSON.parse(data.data);
cbList[data.cb](data.res);
}
var cbList = {};
cbList.busstopResponse = function (data) {
var html = '';
for (var i = 0; i < data.length; i++)
html += '<div style="cursor: pointer;" onclick="loadBoard(' + data[i].id + ')">' + data[i].name + ', ' + data[i].city + '</div>';
document.getElementById("container").innerHTML = html;
}
cbList.stationboardResponse = function (data) {
console.log("Result:", data);
var html = '';
for (var i = 0; i < data.length; i++)
html += '<div>' +
data[i].number + ', ' + data[i].destination +
' at ' +
(new Date(data[i].departure)).toLocaleString() +
((data[i].delay != undefined) ? (' with a delay of ' + data[i].delay + ' min') : '' ) +
'</div>';
document.getElementById("container").innerHTML = html;
}
function loadBoard(id) {
console.log("Request board for: " + id);
ws.send(JSON.stringify({call:"stationboardRequest", query:id}));
}