|
|
@ -1609,135 +1609,138 @@ function tr(key) { return _tr[key] || '?'+key+'?'; } |
|
|
|
w.startScanning = startScanning; |
|
|
|
w.startScanning = startScanning; |
|
|
|
})(window.WiFi = {}); |
|
|
|
})(window.WiFi = {}); |
|
|
|
/** Handle connections */ |
|
|
|
/** Handle connections */ |
|
|
|
var Conn = (function() { |
|
|
|
var Conn = (function () { |
|
|
|
var ws; |
|
|
|
var ws; |
|
|
|
var heartbeatTout; |
|
|
|
var heartbeatTout; |
|
|
|
var pingIv; |
|
|
|
var pingIv; |
|
|
|
var xoff = false; |
|
|
|
var xoff = false; |
|
|
|
var autoXoffTout; |
|
|
|
var autoXoffTout; |
|
|
|
|
|
|
|
var reconTout; |
|
|
|
function onOpen(evt) { |
|
|
|
|
|
|
|
console.log("CONNECTED"); |
|
|
|
var pageShown = false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function onOpen(evt) { |
|
|
|
function onClose(evt) { |
|
|
|
console.log("CONNECTED"); |
|
|
|
console.warn("SOCKET CLOSED, code "+evt.code+". Reconnecting..."); |
|
|
|
doSend("i"); |
|
|
|
setTimeout(function() { |
|
|
|
} |
|
|
|
init(); |
|
|
|
|
|
|
|
}, 200); |
|
|
|
|
|
|
|
// this happens when the buffer gets fucked up via invalid unicode.
|
|
|
|
|
|
|
|
// we basically use polling instead of socket then
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function onMessage(evt) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
// . = heartbeat
|
|
|
|
|
|
|
|
switch (evt.data.charAt(0)) { |
|
|
|
|
|
|
|
case 'B': |
|
|
|
|
|
|
|
case 'T': |
|
|
|
|
|
|
|
case 'S': |
|
|
|
|
|
|
|
Screen.load(evt.data); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case '-': |
|
|
|
|
|
|
|
//console.log('xoff');
|
|
|
|
|
|
|
|
xoff = true; |
|
|
|
|
|
|
|
autoXoffTout = setTimeout(function(){xoff=false;}, 250); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case '+': |
|
|
|
|
|
|
|
//console.log('xon');
|
|
|
|
|
|
|
|
xoff = false; |
|
|
|
|
|
|
|
clearTimeout(autoXoffTout); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
heartbeat(); |
|
|
|
|
|
|
|
} catch(e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function canSend() { |
|
|
|
function onClose(evt) { |
|
|
|
return !xoff; |
|
|
|
console.warn("SOCKET CLOSED, code " + evt.code + ". Reconnecting..."); |
|
|
|
} |
|
|
|
clearTimeout(reconTout); |
|
|
|
|
|
|
|
reconTout = setTimeout(function () { |
|
|
|
|
|
|
|
init(); |
|
|
|
|
|
|
|
}, 2000); |
|
|
|
|
|
|
|
// this happens when the buffer gets fucked up via invalid unicode.
|
|
|
|
|
|
|
|
// we basically use polling instead of socket then
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function doSend(message) { |
|
|
|
function onMessage(evt) { |
|
|
|
if (_demo) { |
|
|
|
try { |
|
|
|
console.log("TX: ", message); |
|
|
|
// . = heartbeat
|
|
|
|
return true; // Simulate success
|
|
|
|
switch (evt.data.charAt(0)) { |
|
|
|
} |
|
|
|
case 'B': |
|
|
|
if (xoff) { |
|
|
|
case 'T': |
|
|
|
// TODO queue
|
|
|
|
case 'S': |
|
|
|
console.log("Can't send, flood control."); |
|
|
|
Screen.load(evt.data); |
|
|
|
return false; |
|
|
|
if(!pageShown) { |
|
|
|
} |
|
|
|
showPage(); |
|
|
|
|
|
|
|
pageShown = true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case '-': |
|
|
|
|
|
|
|
//console.log('xoff');
|
|
|
|
|
|
|
|
xoff = true; |
|
|
|
|
|
|
|
autoXoffTout = setTimeout(function () { |
|
|
|
|
|
|
|
xoff = false; |
|
|
|
|
|
|
|
}, 250); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case '+': |
|
|
|
|
|
|
|
//console.log('xon');
|
|
|
|
|
|
|
|
xoff = false; |
|
|
|
|
|
|
|
clearTimeout(autoXoffTout); |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
heartbeat(); |
|
|
|
|
|
|
|
} catch (e) { |
|
|
|
|
|
|
|
console.error(e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!ws) return false; // for dry testing
|
|
|
|
function canSend() { |
|
|
|
if (ws.readyState != 1) { |
|
|
|
return !xoff; |
|
|
|
console.error("Socket not ready"); |
|
|
|
} |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (typeof message != "string") { |
|
|
|
|
|
|
|
message = JSON.stringify(message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ws.send(message); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function init() { |
|
|
|
function doSend(message) { |
|
|
|
if (_demo) { |
|
|
|
if (_demo) { |
|
|
|
console.log("Demo mode!"); |
|
|
|
console.log("TX: ", message); |
|
|
|
Screen.load(_demo_screen); |
|
|
|
return true; // Simulate success
|
|
|
|
showPage(); |
|
|
|
} |
|
|
|
return; |
|
|
|
if (xoff) { |
|
|
|
} |
|
|
|
// TODO queue
|
|
|
|
heartbeat(); |
|
|
|
console.log("Can't send, flood control."); |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
ws = new WebSocket("ws://"+_root+"/term/update.ws"); |
|
|
|
if (!ws) return false; // for dry testing
|
|
|
|
ws.onopen = onOpen; |
|
|
|
if (ws.readyState != 1) { |
|
|
|
ws.onclose = onClose; |
|
|
|
console.error("Socket not ready"); |
|
|
|
ws.onmessage = onMessage; |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (typeof message != "string") { |
|
|
|
|
|
|
|
message = JSON.stringify(message); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ws.send(message); |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
console.log("Opening socket."); |
|
|
|
function init() { |
|
|
|
|
|
|
|
if (_demo) { |
|
|
|
|
|
|
|
console.log("Demo mode!"); |
|
|
|
|
|
|
|
Screen.load(_demo_screen); |
|
|
|
|
|
|
|
showPage(); |
|
|
|
|
|
|
|
return; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Ask for initial data
|
|
|
|
clearTimeout(reconTout); |
|
|
|
$.get('http://'+_root+'/term/init', function(resp, status) { |
|
|
|
clearTimeout(heartbeatTout); |
|
|
|
if (status !== 200) location.reload(true); |
|
|
|
|
|
|
|
console.log("Data received!"); |
|
|
|
|
|
|
|
Screen.load(resp); |
|
|
|
|
|
|
|
heartbeat(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
showPage(); |
|
|
|
ws = new WebSocket("ws://" + _root + "/term/update.ws"); |
|
|
|
}); |
|
|
|
ws.onopen = onOpen; |
|
|
|
} |
|
|
|
ws.onclose = onClose; |
|
|
|
|
|
|
|
ws.onmessage = onMessage; |
|
|
|
|
|
|
|
console.log("Opening socket."); |
|
|
|
|
|
|
|
heartbeat(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function heartbeat() { |
|
|
|
function heartbeat() { |
|
|
|
clearTimeout(heartbeatTout); |
|
|
|
clearTimeout(heartbeatTout); |
|
|
|
heartbeatTout = setTimeout(heartbeatFail, 2000); |
|
|
|
heartbeatTout = setTimeout(heartbeatFail, 2000); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function heartbeatFail() { |
|
|
|
function heartbeatFail() { |
|
|
|
console.error("Heartbeat lost, probing server..."); |
|
|
|
console.error("Heartbeat lost, probing server..."); |
|
|
|
pingIv = setInterval(function() { |
|
|
|
pingIv = setInterval(function () { |
|
|
|
console.log("> ping"); |
|
|
|
console.log("> ping"); |
|
|
|
$.get('http://'+_root+'/system/ping', function(resp, status) { |
|
|
|
$.get('http://' + _root + '/system/ping', function (resp, status) { |
|
|
|
if (status == 200) { |
|
|
|
if (status == 200) { |
|
|
|
clearInterval(pingIv); |
|
|
|
clearInterval(pingIv); |
|
|
|
console.info("Server ready, reloading page..."); |
|
|
|
console.info("Server ready, reloading page..."); |
|
|
|
location.reload(); |
|
|
|
location.reload(); |
|
|
|
} |
|
|
|
} |
|
|
|
}, { |
|
|
|
}, { |
|
|
|
timeout: 100, |
|
|
|
timeout: 100, |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, 500); |
|
|
|
}, 1000); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return { |
|
|
|
return { |
|
|
|
ws: null, |
|
|
|
ws: null, |
|
|
|
init: init, |
|
|
|
init: init, |
|
|
|
send: doSend, |
|
|
|
send: doSend, |
|
|
|
canSend: canSend, // check flood control
|
|
|
|
canSend: canSend, // check flood control
|
|
|
|
}; |
|
|
|
}; |
|
|
|
})(); |
|
|
|
})(); |
|
|
|
/** |
|
|
|
/** |
|
|
|
* User input |
|
|
|
* User input |
|
|
|