Skip to content

Instantly share code, notes, and snippets.

@louisremi
Created June 15, 2011 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save louisremi/1027192 to your computer and use it in GitHub Desktop.
Save louisremi/1027192 to your computer and use it in GitHub Desktop.
Friends Timeline snippets
// Create an EventSource object,
// passing it the URL of the server sccript
var evtSrc = new EventSource( "server.php" );
// Listen for messages/events on the EventSource
evtSrc.onmessage = function ( e ) {
addMessage( "status", JSON.parse(e.data) );
}
evtSrc.addEventListener("checkin", function( e ) {
addMessage( "checkin", JSON.parse(e.data) );
}, false);
evtSrc.addEventListener("forward", function( e ) {
addMessage( "forward", JSON.parse(e.data) );
}, false);
evtSrc.addEventListener("direct", function( e ) {
addMessage( "direct", JSON.parse(e.data) );
}, false);
// Parse the data and insert the message in the timeline
// with the appropriate className
function addMessage( className, data ) {
...
}
data: {"from": "Sandrine", "msg": "dressed in blue today", "location":"Brussel"}
event: direct
data: {"from": "yann", "msg": "You should check your inbox", "location": "Boston"}
event: checkin
data: {"from": "Peter", "place": "StarCoffee", "location": "Berlin"}
event: forward
data: {"from": "roland", "through": "Sasha", "msg": "watch this! http://youtu.be/LybAHotsvOg", "location": "Mexico"}
<?php
// Send appropriate mime type
header("Content-Type: text/event-stream\n\n");
// Read messages
$filename = "messages.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
// Split the messages into an array
$messages = preg_split( "/\n\n/", $contents );
// Send one message every 2 to 7 seconds
foreach ( $messages as $message ) {
echo $message;
echo "\n\n";
ob_flush();
flush();
sleep( rand(2, 7) );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment