2017-06-13 01:24:30 +00:00
|
|
|
|
2017-06-13 01:50:48 +00:00
|
|
|
// Create WebSocket connection.
|
2017-06-15 23:18:28 +00:00
|
|
|
const socket = new WebSocket ("ws://" + document.location.host + rootElementPath, "ooui");
|
2017-06-13 01:24:30 +00:00
|
|
|
|
2017-06-15 09:39:19 +00:00
|
|
|
console.log("Web socket created");
|
2017-06-13 04:50:15 +00:00
|
|
|
|
|
|
|
const nodes = {}
|
|
|
|
|
|
|
|
function getNode (id) {
|
|
|
|
switch (id) {
|
|
|
|
case "window": return window;
|
|
|
|
case "document": return document;
|
|
|
|
case "document.body": return document.body;
|
|
|
|
default: return nodes[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function msgCreate (m) {
|
|
|
|
const id = m.id;
|
2017-06-13 07:03:01 +00:00
|
|
|
const tagName = m.k;
|
2017-06-15 07:40:08 +00:00
|
|
|
const node = tagName === "#text" ?
|
2017-06-13 04:50:15 +00:00
|
|
|
document.createTextNode ("") :
|
|
|
|
document.createElement (tagName);
|
2017-06-15 07:40:08 +00:00
|
|
|
if (tagName !== "#text")
|
2017-06-13 04:50:15 +00:00
|
|
|
node.id = id;
|
|
|
|
nodes[id] = node;
|
2017-06-13 07:51:24 +00:00
|
|
|
console.log ("Created node", node);
|
2017-06-13 04:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function msgSet (m) {
|
|
|
|
const id = m.id;
|
|
|
|
const node = getNode (id);
|
|
|
|
if (!node) {
|
2017-06-13 07:51:24 +00:00
|
|
|
console.error ("Unknown node id", m);
|
2017-06-13 04:50:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
node[m.k] = m.v;
|
2017-06-15 09:39:19 +00:00
|
|
|
console.log ("Set", node, m.k, m.v);
|
2017-06-13 04:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function msgCall (m) {
|
|
|
|
const id = m.id;
|
|
|
|
const node = getNode (id);
|
|
|
|
if (!node) {
|
2017-06-13 07:51:24 +00:00
|
|
|
console.error ("Unknown node id", m);
|
2017-06-13 04:50:15 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const f = node[m.k];
|
|
|
|
console.log ("Call", node, f, m.v);
|
|
|
|
f.apply (node, m.v);
|
|
|
|
}
|
|
|
|
|
2017-06-15 07:40:08 +00:00
|
|
|
function msgListen (m) {
|
2017-06-15 07:58:55 +00:00
|
|
|
const node = getNode (m.id);
|
|
|
|
if (!node) {
|
|
|
|
console.error ("Unknown node id", m);
|
|
|
|
return;
|
|
|
|
}
|
2017-06-15 09:39:19 +00:00
|
|
|
console.log ("Listen", node, m.k);
|
2017-06-15 07:58:55 +00:00
|
|
|
node.addEventListener(m.k, function () {
|
|
|
|
const em = {
|
|
|
|
m: "event",
|
|
|
|
id: m.id,
|
|
|
|
k: m.k,
|
|
|
|
};
|
2017-06-16 06:43:16 +00:00
|
|
|
if (m.k === "change") {
|
2017-06-16 06:27:07 +00:00
|
|
|
em.v = node.value;
|
|
|
|
}
|
2017-06-15 07:58:55 +00:00
|
|
|
const ems = JSON.stringify (em);
|
|
|
|
socket.send (ems);
|
2017-06-16 06:27:07 +00:00
|
|
|
console.log ("Event", em);
|
2017-06-15 07:58:55 +00:00
|
|
|
});
|
2017-06-15 07:40:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 04:50:15 +00:00
|
|
|
function processMessage (m) {
|
|
|
|
switch (m.m) {
|
2017-06-13 07:03:01 +00:00
|
|
|
case "nop":
|
|
|
|
break;
|
|
|
|
case "create":
|
2017-06-13 04:50:15 +00:00
|
|
|
msgCreate (m);
|
|
|
|
break;
|
2017-06-13 07:03:01 +00:00
|
|
|
case "set":
|
2017-06-13 04:50:15 +00:00
|
|
|
msgSet (m);
|
|
|
|
break;
|
2017-06-13 07:03:01 +00:00
|
|
|
case "call":
|
2017-06-13 04:50:15 +00:00
|
|
|
msgCall (m);
|
|
|
|
break;
|
2017-06-15 07:40:08 +00:00
|
|
|
case "listen":
|
|
|
|
msgListen (m);
|
|
|
|
break;
|
2017-06-13 04:50:15 +00:00
|
|
|
default:
|
|
|
|
console.error ("Unknown message type", m.m, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function fixupValue (v) {
|
|
|
|
if (Array.isArray (v)) {
|
|
|
|
for (x in v) {
|
|
|
|
v[x] = fixupValue (v[x]);
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
else if (typeof v === 'string' || v instanceof String) {
|
2017-06-15 09:39:19 +00:00
|
|
|
if ((v.length > 1) && (v[0] === "\u2999")) {
|
2017-06-13 07:03:01 +00:00
|
|
|
// console.log("V", v);
|
2017-06-15 09:39:19 +00:00
|
|
|
return getNode (v);
|
2017-06-13 04:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2017-06-13 01:24:30 +00:00
|
|
|
|
2017-06-13 01:50:48 +00:00
|
|
|
socket.addEventListener('open', function (event) {
|
2017-06-15 09:39:19 +00:00
|
|
|
console.log("Web socket opened");
|
2017-06-13 01:50:48 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.addEventListener('message', function (event) {
|
2017-06-13 04:50:15 +00:00
|
|
|
const message = JSON.parse (event.data);
|
2017-06-15 09:39:19 +00:00
|
|
|
// console.log('Raw value from server', message.v);
|
2017-06-13 04:50:15 +00:00
|
|
|
message.v = fixupValue (message.v);
|
2017-06-15 09:39:19 +00:00
|
|
|
// console.log('Message from server', message);
|
2017-06-13 04:50:15 +00:00
|
|
|
processMessage (message);
|
2017-06-13 01:50:48 +00:00
|
|
|
});
|