Created
May 27, 2010 01:05
-
-
Save rwaldron/415294 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="event-source-2.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener('DOMContentLoaded', function () { | |
var eventSrc = new EventSource('event-source-2.php'); | |
console.group('EventSource Created'); | |
console.dir( eventSrc ); | |
console.groupEnd(); | |
eventSrc.addEventListener('open', function (event) { | |
console.time('Time Elapsed between open and message'); | |
console.log(event.type); | |
}); | |
eventSrc.addEventListener('message', function (event) { | |
console.timeEnd('Time Elapsed between open and message'); | |
console.log(event); | |
console.dir( JSON.parse(event.data) ); | |
}); | |
}, false); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
header("Content-Type: text/event-stream\n\n"); | |
// despite not having the while(true){} | |
// this seems to repeat pushing messages to the client | |
echo 'data: ' . json_encode( | |
array( | |
0 => array( | |
'time' => time(), | |
'message' => 'Some kind of foo' | |
), | |
1 => array( | |
'time' => time(), | |
'message' => 'Some kind of quux' | |
) | |
) | |
) . "\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment