2014-03-12 21:45:35 +00:00
|
|
|
var PropertyChangeListenerObjects = {
|
2014-03-14 20:15:51 +00:00
|
|
|
_ws : null,
|
|
|
|
_listeners : new Array()
|
2014-03-12 21:45:35 +00:00
|
|
|
};
|
|
|
|
|
2014-03-14 20:15:51 +00:00
|
|
|
var PropertyChangeListener = function(callback) {
|
2014-03-12 21:45:35 +00:00
|
|
|
PropertyChangeListenerObjects._ws = new WebSocket('ws://' + location.host + '/PropertyListener');
|
|
|
|
PropertyChangeListenerObjects._ws.onopen = callback;
|
|
|
|
PropertyChangeListenerObjects._ws.onclose = function(ev) {
|
2014-03-14 20:15:51 +00:00
|
|
|
alert('Lost connection to FlightGear. Please reload this page and/or restart FlightGear.');
|
|
|
|
PropertyChangeListenerObjects._ws = null;
|
|
|
|
};
|
|
|
|
PropertyChangeListenerObjects._ws.onerror = function(ev) {
|
|
|
|
alert('Error communicating with FlightGear. Please reload this page and/or restart FlightGear.');
|
2014-03-12 21:45:35 +00:00
|
|
|
PropertyChangeListenerObjects._ws = null;
|
|
|
|
};
|
|
|
|
PropertyChangeListenerObjects._ws.onmessage = function(ev) {
|
|
|
|
try {
|
|
|
|
var node = JSON.parse(ev.data);
|
|
|
|
var cb = PropertyChangeListenerObjects._listeners[node.path];
|
2014-03-14 20:15:51 +00:00
|
|
|
for (var i = 0; i < cb.length; i++)
|
2014-03-12 21:45:35 +00:00
|
|
|
cb[i](node);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2014-03-14 20:15:51 +00:00
|
|
|
var SetListener = function(path, callback) {
|
2014-03-12 21:45:35 +00:00
|
|
|
var o = PropertyChangeListenerObjects._listeners[path];
|
|
|
|
if (typeof (o) == 'undefined') {
|
|
|
|
o = new Array();
|
|
|
|
PropertyChangeListenerObjects._listeners[path] = o;
|
|
|
|
PropertyChangeListenerObjects._ws.send(JSON.stringify({
|
|
|
|
command : 'addListener',
|
|
|
|
node : path
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
o.push(callback);
|
|
|
|
};
|