Add debug facilities in messaging module toggled by GET parameter

This commit is contained in:
Tissevert 2019-08-13 13:46:25 +02:00
parent b2253b81d6
commit ac114c16df

View file

@ -1,5 +1,7 @@
function Messaging(screen) {
var ws = new WebSocket('ws://' + window.location.hostname + '/play/');
var debug = getParameters().debug;
var doLog = debug != undefined && debug.match(/^(?:1|t(?:rue)?|v(?:rai)?)$/i);
var keepAlivePeriod = 20000;
var routes = {callbacks: [], children: {}};
@ -25,6 +27,15 @@ function Messaging(screen) {
}
}
function getParameters() {
var o = {};
window.location.search.substr(1).split('&').forEach(function(s) {
var t = s.split('=');
o[t[0]] = t[1];
});
return o;
}
function addEventListener(path, callback) {
var route = get(routes, path, true);
route.callbacks.push(callback);
@ -44,8 +55,16 @@ function Messaging(screen) {
} else {
debug.textContent = event.data;
}
o.direction = 'client < server';
log(o);
};
function log(message) {
if(doLog) {
console.log(message);
}
}
function start() {
ping();
addEventListener(["Pong"], ping);
@ -54,6 +73,8 @@ function Messaging(screen) {
function send(o) {
ws.send(JSON.stringify(o));
o.direction = 'client > server';
log(o);
}
function ping() {