Created
August 27, 2010 00:53
-
-
Save rwaldron/552549 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
<?php | |
header("Content-Type: text/event-stream\n\n"); | |
echo 'data: ' . json_encode( | |
array( | |
'from' => 'event-source' | |
) | |
) . "\n"; | |
?> |
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="renderer.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
var worker = new Worker('worker.js'); | |
worker.addEventListener('message', function (event) { | |
var message = event.data; | |
console.group('Message Received'); | |
console.log( message ); | |
console.groupEnd(); | |
}, false); | |
// This will trigger a message event in the worker | |
worker.postMessage(true); |
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
self.addEventListener('message', function (event) { | |
var thisp = this, | |
eventSrc = new EventSource('event-source.php'); | |
eventSrc.addEventListener('message', function (event) { | |
var message = JSON.parse(event.data); | |
thisp.postMessage(message); | |
}, false); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Rick,
I took your code and did a copy and paste but I can't seem to get it to work. I keep getting this error in the console.
ReferenceError: EventSource is not defined
. This error reference line 5 in the worker.js file.I appreciate your help and I am trying my best to implement EventSource into a worker.