compiled boot.js

This commit is contained in:
David Botton 2021-03-18 18:47:37 -04:00
parent 0316326f91
commit 5cb8377e3c
3 changed files with 152 additions and 131 deletions

View file

@ -240,20 +240,31 @@ the default answer. (Private)"
(defun initialize (on-connect-handler
&key
(host "0.0.0.0")
(port 8080)
(boot-file "/boot.html")
(static-root #P"./static-files/"))
(host "0.0.0.0")
(port 8080)
(boot-file "/boot.html")
(static-boot-js nil)
(static-root #P"./static-files/"))
"Initialize CLOG on a socket using HOST and PORT to serve BOOT-FILE as
the default route for '/' to establish web-socket connections and static files
located at STATIC-ROOT. If BOOT-FILE is nil no initial clog-path's will be
setup, use clog-path to add. The on-connect-handler needs to indentify the
path by querying the browser. See PATH-NAME (in CLOG-LOCATION)."
path by querying the browser. See PATH-NAME (in CLOG-LOCATION). If
static-boot-js is nil then boot.js is served from the file /js/boot.js
instead of the compiled version."
(set-on-connect on-connect-handler)
(when boot-file
(set-clog-path "/" boot-file))
(setf *app*
(lack:builder
(lack:builder
(lambda (app)
(lambda (env)
;; if not static-boot-js use internal compiled boot.js
(if (and (eq static-boot-js nil)
(equalp (getf env :path-info) "/js/boot.js"))
`(200 (:content-type "text/javascript")
(,(compiled-boot-js)))
(funcall app env))))
(lambda (app)
(lambda (env)
;; Special handling of "clog paths"
@ -291,6 +302,9 @@ path by querying the browser. See PATH-NAME (in CLOG-LOCATION)."
(setf *client-handler* (clack:clackup *app* :address host :port port))
(format t "HTTP listening on : ~A:~A~%" host port)
(format t "HTML Root : ~A~%" static-root)
(format t "Boot js source : ~A~%" (if static-boot-js
"actual file"
"compiled in"))
(format t "Boot file for path / : ~A~%" boot-file))
;;;;;;;;;;;;;;;;;;;
@ -448,4 +462,125 @@ HTML <br />."
the browser contents in case of connection loss."
(execute connection-id (format nil "clog['html_on_close']='~A'"
(escape-string html))))
;;;;;;;;;;;;;;;;;;;;;;
;; compiled-boot-js ;;
;;;;;;;;;;;;;;;;;;;;;;
(defun compiled-boot-js ()
"Returns a compiled version of current version of boot.js (private)"
"var ws;
var adr;
var params={};
var clog={};
var pingerid;
if (typeof clog_debug == 'undefined') {
clog_debug = false;
}
function Ping_ws() {
if (ws.readyState == 1) {
ws.send ('0');
}
}
function Shutdown_ws(event) {
if (ws != null) {
ws.onerror = null;
ws.onclose = null;
ws.close ();
ws = null;
}
clearInterval (pingerid);
if (clog['html_on_close'] != '') {
$(document.body).html(clog['html_on_close']);
}
}
function Setup_ws() {
ws.onmessage = function (event) {
try {
if (clog_debug == true) {
console.log ('eval data = ' + event.data);
}
eval (event.data);
} catch (e) {
console.error (e.message);
}
}
ws.onerror = function (event) {
console.log ('onerror: reconnect');
ws = null;
ws = new WebSocket (adr + '?r=' + clog['connection_id']);
ws.onopen = function (event) {
console.log ('onerror: reconnect successful');
Setup_ws();
}
ws.onclose = function (event) {
console.log ('onerror: reconnect failure');
Shutdown_ws(event);
}
}
ws.onclose = function (event) {
console.log ('onclose: reconnect');
ws = null;
ws = new WebSocket (adr + '?r=' + clog['connection_id']);
ws.onopen = function (event) {
console.log ('onclose: reconnect successful');
Setup_ws();
}
ws.onclose = function (event) {
console.log ('onclose: reconnect failure');
Shutdown_ws(event);
}
}
}
$( document ).ready(function() {
var s = document.location.search;
var tokens;
var r = /[?&]?([^=]+)=([^&]*)/g;
clog['body']=document.body;
clog['head']=document.head;
clog['documentElement']=document.documentElement;
clog['window']=window;
clog['navigator']=navigator;
clog['document']=window.document;
clog['location']=window.location;
s = s.split('+').join(' ');
while (tokens = r.exec(s)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
if (location.protocol == 'https:') {
adr = 'wss://' + location.hostname;
} else {
adr = 'ws://' + location.hostname;
}
if (location.port != '') { adr = adr + ':' + location.port; }
adr = adr + '/clog';
try {
console.log ('connecting to ' + adr);
ws = new WebSocket (adr);
} catch (e) {
console.log ('trying again, connecting to ' + adr);
ws = new WebSocket (adr);
}
if (ws != null) {
ws.onopen = function (event) {
console.log ('connection successful');
Setup_ws();
}
pingerid = setInterval (function () {Ping_ws ();}, 10000);
} else {
document.writeln ('If you are seeing this your browser or your connection to the internet is blocking websockets.');
}
});")

View file

@ -47,6 +47,7 @@ same as the clog directy this overides the relative paths used in them.")
(host "0.0.0.0")
(port 8080)
(boot-file "/boot.html")
(static-boot-js nil)
(static-root (merge-pathnames "./static-files/"
(asdf:system-source-directory :clog))))
"Inititalize CLOG on a socket using HOST and PORT to serve BOOT-FILE
@ -56,19 +57,20 @@ shut down, this function does the same as set-on-new-window (does not
change the static-root). STATIC-ROOT by default is the \"directory CLOG
is installed in ./static-files\" If the variable clog:*overide-static-root*
is set STATIC-ROOT will be ignored. If BOOT-FILE is nil no default
boot-file will be set for root path, i.e. /."
boot-file will be set for root path, i.e. /. If static-boot-js is t
then boot.js is served from the file /js/boot.js instead of the
compiled version."
(set-on-new-window on-new-window-handler :path "/" :boot-file boot-file)
(unless *clog-running*
(setf *clog-running* t)
(cc:initialize #'on-connect
:host host
:port port
:boot-file boot-file
:static-root (if *overide-static-root*
*overide-static-root*
static-root))))
:host host
:port port
:boot-file boot-file
:static-boot-js static-boot-js
:static-root (if *overide-static-root*
*overide-static-root*
static-root))))
;;;;;;;;;;;;;;;;;;;;;;;
;; set-on-new-window ;;

View file

@ -1,116 +0,0 @@
var ws;
var adr;
var params={};
var clog={};
var pingerid;
if (typeof clog_debug == 'undefined') {
clog_debug = false;
}
function Ping_ws() {
if (ws.readyState == 1) {
ws.send ("0");
}
}
function Shutdown_ws(event) {
if (ws != null) {
ws.onerror = null;
ws.onclose = null;
ws.close ();
ws = null;
}
clearInterval (pingerid);
if (clog['html_on_close'] != "") {
$(document.body).html(clog['html_on_close']);
}
}
function Setup_ws() {
ws.onmessage = function (event) {
try {
if (clog_debug == true) {
console.log ("eval data = " + event.data);
}
eval (event.data);
} catch (e) {
console.error (e.message);
}
}
ws.onerror = function (event) {
console.log ("onerror: reconnect");
ws = null;
ws = new WebSocket (adr + "?" + clog['connection_id']);
ws.onopen = function (event) {
console.log ("onerror: reconnect successful");
Setup_ws();
}
ws.onclose = function (event) {
console.log ("onerror: reconnect failure");
Shutdown_ws(event);
}
}
ws.onclose = function (event) {
console.log ("onclose: reconnect");
ws = null;
ws = new WebSocket (adr + "?" + clog['connection_id']);
ws.onopen = function (event) {
console.log ("onclose: reconnect successful");
Setup_ws();
}
ws.onclose = function (event) {
console.log ("onclose: reconnect failure");
Shutdown_ws(event);
}
}
}
$( document ).ready(function() {
var s = document.location.search;
var tokens;
var r = /[?&]?([^=]+)=([^&]*)/g;
clog['body']=document.body;
clog['head']=document.head;
clog['documentElement']=document.documentElement;
clog['window']=window;
clog['navigator']=navigator;
clog['document']=window.document;
clog['location']=window.location;
s = s.split("+").join(" ");
while (tokens = r.exec(s)) {
params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]);
}
if (location.protocol == "https:") {
adr = "wss://" + location.hostname;
} else {
adr = "ws://" + location.hostname;
}
if (location.port != "") { adr = adr + ":" + location.port; }
adr = adr + "/clog";
try {
console.log ("connecting to " + adr);
ws = new WebSocket (adr);
} catch (e) {
console.log ("trying again, connecting to " + adr);
ws = new WebSocket (adr);
}
if (ws != null) {
ws.onopen = function (event) {
console.log ("connection successful");
Setup_ws();
}
pingerid = setInterval (function () {Ping_ws ();}, 10000);
} else {
document.writeln ("If you are seeing this your browser or your connection to the internet is blocking websockets.");
}
});