dimanche 1 novembre 2020

server sent events unexpected stop

let me first say that I'm no expert in web development so I might have made a stupid mistake but no amount of googling seems to be helping. I need to have a single html page (preprocessed in php) to display occasional events fired by a "server" page residing on the same machine. I've readapted the basic w3schools server sent events example as follows and it seemed to be working fine until last night, but today (still working when first tested) I added a simple table and a ref to an external (empty) js file to the html and events stopped being caught by the page. I decided to roll back to the working code but even that doesn't work anymore. I had to remove the files and restore from a backup! I'm sure the sse.php code is being run since part of what it does is removing records from a sqlite3 database and that's happening. Here's the code, I really hope you can help me because I really have no idea what's happening. This was tested on Linux + Xampp + Firefox and sadly this is a mandatory combination, having it work under other conditions is not useful at the moment. (fyi: opening the mon.php page in Opera gave me a single event and then stopped working as well) Thank you all.

mon.php

<?php
    if ( ! isset($_GET['mon']) ) {
        die('mon code missing');
    }
?>
<html>
<head>
    <script type="text/javascript">
        var evtSourceUrl = "sse.php?mon=" + <?php echo '"'.$_GET['mon'].'"'?>;
        if(typeof(EventSource) !== "undefined") {
            var source = new EventSource(evtSourceUrl);
            source.onmessage = function(event) {
                document.getElementById("info").innerHTML += event.data + "<br>";
            };
        } else {
            document.getElementById("info").innerHTML = 'sse not supported';
        }
    </script>
</head>
<body>
    <p>
        <span id="info"></span>
    </p>
</body>
</html>

sse.php

<?php
    function output_sse($msg) {
        echo "data: " . $msg . "\n\n";
        ob_end_flush();
        flush();
    }

    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    header('Connection: keep-alive');

    $mon = null;
    if ( isset($_GET["mon"]) ) {
        $mon = $_GET["mon"];
    } else {
        output_sse('mon code missing');
        die();
    }

    while ( true ) {
        usleep(1000 * 100);
        $db = new PDO("sqlite:ch.sqlite3");
        if ( $db === false ) {
          output_sse('err:PDO');
          die();
        }
        $stmt_select = $db->query("SELECT * FROM CH WHERE IDMON='$mon';");
        $stmt_delete = $db->prepare("DELETE FROM CH WHERE IDMON='$mon';");
        $db->beginTransaction();
        $res = $stmt_select->fetch();
        $stmt_delete->execute();
        $db->commit();
        $db->close();
        if ( strlen($res['TK']) > 0 && strlen($res['SP']) > 0 ) {
          $msg = "$res['TK'] :: $res['SP']";
          output_sse($msg);
        }
    }
?>

add.php (to add new records to be displayed - this works)

<?php
    $mon = isset($_GET["mon"]) ? $_GET["mon"] : die ('mon code missing') ;
    $tk = isset($_GET["tk"]) ? $_GET["tk"] : die ('tk code missing') ;
    $sp = isset($_GET["sp"]) ? $_GET["sp"] : die ('sp code missing') ;

    $db = new SQLite3('ch.sqlite3');

    if ( $db === false ) {
        die('Cannot open db');
    }

    $res = $db->exec("INSERT INTO CH('IDMON','TK','SP') VALUES('$mon','$tk','$sp');");
    if ( $res != 1 ) {
        echo 'insert failed';
    }

    if ( $db->close() === false ) {
        die('Cannot close connection to db');
    }
    echo('ok');
?>



Aucun commentaire:

Enregistrer un commentaire