Created
June 15, 2011 14:15
-
-
Save louisremi/1027192 to your computer and use it in GitHub Desktop.
Friends Timeline snippets
This file contains hidden or 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
// 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 ) { | |
... | |
} |
This file contains hidden or 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
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"} |
This file contains hidden or 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 | |
// 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