-
-
Save rwaldron/1029280 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" ), | |
// Setup event types, provide explicit values if nec. | |
eventTypes = { | |
message: "status", | |
checkin: 1, | |
forward: 1, | |
direct: 1 | |
}; | |
// Create listeners for each event type | |
[].forEach.call( Object.keys( eventTypes ), function( type ) { | |
var eventType = eventTypes[ type ], | |
// determine if the className should be overridden by explicit value | |
className = eventType === 1 ? eventType : type; | |
evtSrc.addEventListener( type, function( e ) { | |
addMessage( className, JSON.parse(e.data) ); | |
}, false); | |
}); | |
// Parse the data and insert the message in the timeline | |
// with the appropriate className | |
function addMessage( className, data ) { | |
//console.log( 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