Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP - Question about the correct flow to use.

Options
  • 19-11-2014 2:57pm
    #1
    Registered Users Posts: 52 ✭✭


    Hello,

    I am writing some PHP that will connect to a socket, obtain readings in realtime and push those readings to a HTML5 page using SSE.

    The problem I have is I need two loops. One to keep the connection to the socket going and one loop to keep the connection to the page going for SSE.

    I tried getting around this by writing the code to connect to the socket in the SSE loop However, It's not going through the code as I expect it to.

    It's probably something stupid that I'm doing wrong. Can anyone point me in the right direction please?

    SSE is short for Server Sent Events. They are supported in HTML5 primarily on Chrome.

    My code follows:
    [php]
    <?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    $startedAt = time();

    function sendTime($id , $msg) {
    echo "event: servertime" . PHP_EOL;
    echo "id: $id" . PHP_EOL;
    echo "data: {\n";
    echo "data: \"curtime\": \"$msg\", \n";
    echo "data: \"id\": $id\n";
    echo "data: }\n";
    echo PHP_EOL;
    ob_flush();
    flush();
    }

    function sendTemperature($id , $msg , $msg1) {
    echo "event: envirocheckTemperature" . PHP_EOL;
    echo "id: $id" . PHP_EOL;
    echo "data: {\n";
    echo "data: \"curtemp\": \"$msg\"\n";
    echo "data: }\n";
    echo PHP_EOL;
    echo "event: envirocheckPressure" . PHP_EOL;
    echo "id: $id" . PHP_EOL;
    echo "data: {\n";
    echo "data: \"curpres\": \"$msg1\"\n";
    echo "data: }\n";
    echo PHP_EOL;
    ob_flush();
    flush();
    }

    function ProcessTemperature($strTemperatureString) {
    $intCurrentTemperature = preg_replace("/[^0-9\.]/", '', $strTemperatureString);
    $intCurrentTemperature = (float)$intCurrentTemperature;
    sendTemperature($thisdate, $strTemperatureString);
    echo($thisdate. $strTemperatureString);
    }

    function ProcessPressure($strTemperatureString) {
    $intCurrentPressure = preg_replace("/[^0-9\.]/", '', $strPressureString);
    $intCurrentPressure = (float)$intCurrentPressure;
    sendPressure($thisdate, $strPressureString);
    echo($thisdate. $strPressureString);
    }

    if(!($sock = socket_create(AF_INET, SOCK_STREAM, 0)))
    {
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);
    die("Couldn't create socket: [$errorcode] $errormsg \n");
    }
    echo "Socket created \n";
    if(!socket_connect($sock , 'rp' , 5007))
    {
    $errorcode = socket_last_error();
    $errormsg = socket_strerror($errorcode);

    die("Could not connect: [$errorcode] $errormsg \n");
    }
    echo "Connection established \n";

    //Receive reply from server
    while($output = socket_read ( $sock , 30 , PHP_NORMAL_READ)) {
    if (strpos($output,'Temperature') == true){
    ProcessTemperature($output);
    }elseif (strpos($output,'Pressure') == true){
    ProcessPressure($output);
    }
    if ((time() - $startedAt) > 10) {
    die();
    }
    sendTime($startedAt , time());
    sleep(1);
    }
    socket_close($sock);
    ?>[/php]
    Tagged:


Comments

  • Registered Users Posts: 6,028 ✭✭✭Talisman


    Why not simplify things and break the process in two?
    The first process reads the value and caches it (Redis / Memcache).
    The second process reads the cached value and sends it to the client.


Advertisement