Created
May 24, 2015 10:01
-
-
Save mdmunir/01529ef854328cf26ff7 to your computer and use it in GitHub Desktop.
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 | |
| /* @var $this \yii\web\View */ | |
| ?> | |
| <div> | |
| Event: | |
| <ul id="response"> | |
| </ul> | |
| </div> | |
| <?php | |
| $url = \yii\helpers\Url::to(['/site/message'],true); | |
| $js = <<<JS | |
| var evtSource = new EventSource("$url"); | |
| evtSource.onmessage = function(e) { | |
| $('<li>').css({color:'red'}).text(e.data).appendTo('#response'); | |
| } | |
| evtSource.addEventListener("ping", function(e) { | |
| var obj = JSON.parse(e.data); | |
| $('<li>').text("ping at " + obj.time).appendTo('#response'); | |
| }); | |
| JS; | |
| $this->registerJs($js); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment