Javascript: Using server-sent events with PHP? -
i'm trying first time use server sent events
inmy project , i'm quite excited it. because can goodbye ajax. ( love ajax not i'm doing now).
any way, tried use server sent events check ping server (php script) , if ping happens, show simple alert('hello world'); in page.
however, don't know why never in page.. no errors either indicate went wrong.
this javascript server sent code:
var pin =localstorage.getitem("pin"); var evtsource = new eventsource("https://example.com/check-for-orders.php?pin="+pin+""); evtsource.addeventlistener("ping", function(e) { var data = json.parse(e.data); alert(data); alert('hello world'); }, false);
and php code:
<?php header("access-control-allow-origin: *"); header("content-type: text/event-stream"); header("cache-control: no-cache"); //header("connection: keep-alive"); session_start(); //don't forget enable mod_headers module in httpd.conf. include "../config/connect.php"; // block grabs whole list viewing $pin = $_get['pin']; $pin2 = $pin.', '; $sql = "select * orders something=0 limit 1"; $query = mysqli_query($db_conx, $sql); $productcount2 = mysqli_num_rows($query ); if ($productcount2 > 0) { while($row = mysqli_fetch_array($query, mysqli_assoc)){ $id2 = $row["id"]; $full_address = $row["full_address"]; $drop_address = $row["drop_address"]; $uphone = $row["uphone"]; $ulat = $row["ulat"]; $ulng = $row["ulng"]; $order_id = $row["order_id"]; echo "event: ping\n"; echo "".$order_id." - pick location: ". $full_address .""; echo "<br /><br />"; echo "drop location: ".$drop_address.", phone: ".$uphone.""; } } else { } mysqli_close($db_conx); ?>
could please advise on issue?
any appreciated.
you need put code in try.....catch block, if error occuers show there.
try { header("access-control-allow-origin: *"); header("content-type: text/event-stream"); header("cache-control: no-cache"); session_start(); include "../config/connect.php"; $pin = $_get['pin']; $pin2 = $pin.', '; $sql = "select * orders something=0 limit 1"; $query = mysqli_query($db_conx, $sql); $productcount2 = mysqli_num_rows($query ); if ($productcount2 > 0) { while($row = mysqli_fetch_array($query, mysqli_assoc)) { $id2 = $row["id"]; $full_address = $row["full_address"]; $drop_address = $row["drop_address"]; $uphone = $row["uphone"]; $ulat = $row["ulat"]; $ulng = $row["ulng"]; $order_id = $row["order_id"]; echo "event: ping\n"; echo "".$order_id." - pick location: ". $full_address .""; echo "<br /><br />"; echo "drop location: ".$drop_address.", phone: ".$uphone.""; } } else { } mysqli_close($db_conx); } catch (exception $e) { echo 'caught exception: ', $e->getmessage(), "\n"; }
Comments
Post a Comment