In PHP
<?php
header("access-control-allow-origin: *");
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo "data: The server time is: {$time}\n\n";
flush();
?>
in html
<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>
<script>
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("http://standardservices.info/api.standardservices.biz/event_stream.php");
source.onmessage = function(event) {
console.log(event);
document.getElementById("result").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
</body>
</html>
Better Explanation from here http://www.html5rocks.com/en/tutorials/eventsource/basics/
<?php
header("access-control-allow-origin: *");
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo "data: The server time is: {$time}\n\n";
flush();
?>
in html
<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>
<script>
if(typeof(EventSource) !== "undefined") {
var source = new EventSource("http://standardservices.info/api.standardservices.biz/event_stream.php");
source.onmessage = function(event) {
console.log(event);
document.getElementById("result").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}
</script>
</body>
</html>
Better Explanation from here http://www.html5rocks.com/en/tutorials/eventsource/basics/
0 comments:
Post a Comment