Add '12/pipeserver.js'
parent
3797cd4ed2
commit
6ccf8e7c10
@ -0,0 +1,73 @@
|
||||
var ws_addr = 'wss://'+window.location.host+"/sandbox/pipe",
|
||||
sock = null,
|
||||
//shell = document.getElementById("shell"),
|
||||
authors = document.getElementById("authors"),
|
||||
title = document.getElementById("title"),
|
||||
connections = document.getElementById("connections");
|
||||
|
||||
var delay = 10 * 1000;
|
||||
function showtitle(t) {
|
||||
setTimeout(function () {
|
||||
title.innerHTML = t;
|
||||
}, delay);
|
||||
}
|
||||
|
||||
function showauthors(a) {
|
||||
setTimeout(function () {
|
||||
authors.innerHTML = a;
|
||||
}, delay);
|
||||
}
|
||||
|
||||
|
||||
function connect () {
|
||||
sock = new WebSocket(ws_addr);
|
||||
sock.onopen = function (event) {
|
||||
console.log("socket opened");
|
||||
document.body.classList.add("connected");
|
||||
// sock.send(JSON.stringify({
|
||||
// src: "connect",
|
||||
// }));
|
||||
};
|
||||
sock.onmessage = function (event) {
|
||||
// console.log("message", event);
|
||||
if (typeof(event.data) == "string") {
|
||||
var msg = JSON.parse(event.data);
|
||||
// console.log("message JSON", msg);
|
||||
if (msg.src == "stdin" && msg.line) {
|
||||
|
||||
// use a regular expression to parse the line
|
||||
var m = msg.line.match(/^meta:(.*?):(.*)$/);
|
||||
if (m) {
|
||||
console.log("m", m[1], m[2]);
|
||||
if (m[1] == "title") {
|
||||
showtitle(m[2]);
|
||||
}
|
||||
if (m[1] == "authors"){
|
||||
showauthors(m[2]);
|
||||
}
|
||||
} else {
|
||||
//console.log("bad line", msg.line)
|
||||
}
|
||||
|
||||
/*
|
||||
// Show all lines
|
||||
var line = document.createElement("div");
|
||||
line.classList.add("line");
|
||||
line.innerHTML = msg.line;
|
||||
shell.appendChild(line);
|
||||
// scroll to bottom
|
||||
shell.scrollTop = shell.scrollHeight;
|
||||
*/
|
||||
} else if (msg.src == "connect") {
|
||||
connections.innerHTML = msg.connections;
|
||||
}
|
||||
}
|
||||
};
|
||||
sock.onclose = function (event) {
|
||||
// console.log("socket closed");
|
||||
connections.innerHTML = "?";
|
||||
document.body.classList.remove("connected");
|
||||
sock = null;
|
||||
window.setTimeout(connect, 2500);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue