Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 45 additions & 8 deletions www/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,51 @@ $( document ).ready(function() {
break;
}
}
var source = new EventSource('stream.php?session='+session);
source.onmessage = function(event) {
var msg=JSON.parse(event.data);
var path=msg[0];
var msg=msg[1];
//if(path=="startup") postmsg('startup',1);
runmsg(path,msg);
}
var source = null;
var streamReconnectTimer = null;
var streamReconnectDelay = 1000;

function scheduleStreamReconnect() {
if(streamReconnectTimer) return;
if(source) {
source.close();
source = null;
}
streamReconnectTimer = setTimeout(function() {
streamReconnectTimer = null;
connectStream();
}, streamReconnectDelay);
streamReconnectDelay = Math.min(streamReconnectDelay * 2, 30000);
}

function connectStream() {
source = new EventSource('stream.php?session='+encodeURIComponent(session)+'&t='+(new Date()).getTime());
source.onopen = function() {
streamReconnectDelay = 1000;
};
source.onmessage = function(event) {
if(event.data == "closed") {
scheduleStreamReconnect();
return;
}
var data;
try {
data=JSON.parse(event.data);
} catch(error) {
console.log("Invalid stream message", event.data, error);
return;
}
var path=data[0];
var msg=data[1];
//if(path=="startup") postmsg('startup',1);
runmsg(path,msg);
};
source.onerror = function() {
scheduleStreamReconnect();
};
}

connectStream();

$('#body').append($('<div>',{id: 'Firstscreen'}));
$('#body').append($('<div>',{id: 'Secondscreen'}));
Expand Down
15 changes: 13 additions & 2 deletions www/stream.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header('X-Accel-Buffering: no');
@ini_set('zlib.output_compression',0);
@ini_set('implicit_flush',1);
@ob_end_clean();
Expand All @@ -26,13 +27,23 @@ function procmsg($topic,$msg){
}
$topics['hack42bar/output/session/'.$_REQUEST['session'].'/#'] = array("qos"=>0, "function"=>"procmsg");
$mqtt->subscribe($topics,0);
echo "retry: 1000\n";
echo "data: ".json_encode(array("startup","1"))."\n\n";
@flush();
@ob_flush();
$last_ping = time();
while($mqtt->proc()){

if (connection_aborted()) {
break;
}
if ($last_ping < time() - 15) {
echo ": keepalive\n\n";
@flush();
@ob_flush();
$last_ping = time();
}
}
echo "data: closed\n\n";
echo ": closed\n\n";
$mqtt->close();

?>
Loading