1
0
Fork 0

Phi: try to survive flightgear reset

This commit is contained in:
Torsten Dreyer 2015-02-17 13:19:35 +01:00
parent 32d99af20a
commit 82774e0002

View file

@ -20,64 +20,84 @@ require([
function KnockProps(aliases) { function KnockProps(aliases) {
var self = this; var self = this;
this.ws = new WebSocket('ws://' + location.host + '/PropertyListener');
this.ws.onclose = function(ev) {
var msg = 'Lost connection to FlightGear. Please reload this page and/or restart FlightGear.';
alert(msg);
throw new Error(msg);
}
this.ws.onerror = function(ev) { self.initWebsocket = function() {
var msg = 'Error communicating with FlightGear. Please reload this page and/or restart FlightGear.'; self.ws = new WebSocket('ws://' + location.host + '/PropertyListener');
alert(msg);
throw new Error(msg);
}
this.ws.onmessage = function(ev) { self.ws.onclose = function(ev) {
try { var msg = 'Lost connection to FlightGear. Should I try to reconnect?';
self.fire(JSON.parse(ev.data)); if (confirm(msg)) {
} catch (e) { // try reconnect
self.initWebsocket();
} else {
throw new Error(msg);
}
}
self.ws.onerror = function(ev) {
var msg = 'Error communicating with FlightGear. Please reload this page and/or restart FlightGear.';
alert(msg);
throw new Error(msg);
}
self.ws.onmessage = function(ev) {
try {
self.fire(JSON.parse(ev.data));
} catch (e) {
}
};
self.openCache = [];
self.ws.onopen = function(ev) {
// send subscriptions when the socket is open
var c = self.openCache;
delete self.openCache;
console.log(c);
c.forEach(function(e) {
self.addListener(e.prop, e.koObservable);
});
for( var p in self.listeners ) {
self.addListener( p, self.listeners[p] );
}
} }
};
this.openCache = [];
this.ws.onopen = function(ev) {
// send subscriptions when the socket is open
var c = self.openCache;
delete self.openCache;
c.forEach(function(e) {
self.addListener(e.prop, e.koObservable);
});
} }
this.fire = function(json) { self.initWebsocket();
self.fire = function(json) {
var koObservable = self.listeners[json.path] || null; var koObservable = self.listeners[json.path] || null;
if (!koObservable) if (!koObservable)
return; return;
koObservable(json.value); koObservable(json.value);
} }
this.listeners = {} self.listeners = {}
this.addListener = function(prop, koObservable) { self.addListener = function(alias, koObservable) {
if (self.openCache) { if (self.openCache) {
// socket not yet open, just cache the request // socket not yet open, just cache the request
console.log("caching listener request"); console.log("caching listener request");
self.openCache.push({ self.openCache.push({
"prop" : prop, "prop" : alias,
"koObservable" : koObservable "koObservable" : koObservable
}); });
return; return;
} }
var path = this.aliases[prop] || ""; var path = this.aliases[alias] || "";
if (path.length == 0) { if (path.length == 0) {
console.log("can't listen to " + prop + ": unknown alias."); if (alias.charAt(0) == '/') {
return; path = alias;
} else {
console.log("can't listen to " + alias + ": unknown alias.");
return;
}
} }
self.listeners[path] = koObservable; self.listeners[path] = koObservable;
console.log("subscribing to " + alias);
self.ws.send(JSON.stringify({ self.ws.send(JSON.stringify({
command : 'addListener', command : 'addListener',
node : path node : path
@ -88,16 +108,16 @@ require([
})); }));
} }
this.aliases = aliases || {}; self.aliases = aliases || {};
this.setAliases = function(arg) { self.setAliases = function(arg) {
arg.forEach(function(a) { arg.forEach(function(a) {
self.aliases[a[0]] = a[1]; self.aliases[a[0]] = a[1];
}); });
} }
this.props = {}; self.props = {};
this.get = function(target, prop) { self.get = function(target, prop) {
if (self.props[prop]) { if (self.props[prop]) {
return self.props[prop]; return self.props[prop];
} }
@ -117,7 +137,7 @@ require([
return p; return p;
} }
this.write = function(prop, value) { self.write = function(prop, value) {
var path = this.aliases[prop] || ""; var path = this.aliases[prop] || "";
if (path.length == 0) { if (path.length == 0) {
console.log("can't write " + prop + ": unknown alias."); console.log("can't write " + prop + ": unknown alias.");
@ -130,7 +150,7 @@ require([
})); }));
} }
this.propsToObject = function(prop, map, result) { self.propsToObject = function(prop, map, result) {
result = result || {} result = result || {}
prop.children.forEach(function(prop) { prop.children.forEach(function(prop) {
var target = map[prop.name] || null; var target = map[prop.name] || null;