$(function () {
// figure out where we need to connect to, and then do so
var server = $('#connection').text();
var ws = new WebSocket(server);
// Figure out where to log the connection status and setup the hooks to log
// the current status. Also kick off the initial message to the device
var lblStatus = $('#status');
ws.onopen = function () {
lblStatus.text('Connected');
ws.send(
JSON.stringify({
obj: "AV",
num: 1
})
);
};
ws.onclose = function() {
lblStatus.text('Disconnected');
};
ws.onerror = function(e) {
lblStatus.text('Error: ' + e.data);
};
// Parse incoming response and post it to the screen
ws.onmessage = function (msg) {
var res = JSON.parse(msg.data);
if (res.type == "name") {
$("", {
'class': 'objId',
'id' : 'lbl_' + res.obj + '_' + res.num,
'text' : res.obj + res.num
}).appendTo('#container');
$("", {
'class': 'objName',
'id' : 'lblName_' + res.obj + '_' + res.num,
'text' : res.value
}).appendTo('#container');
} else {
$("", {
'class' : 'objPv',
'name' : 'inPv_' + res.obj + '_' + res.num,
'type' : 'text',
'value' : res.value
}).appendTo('#container');
}
};
});