Created
February 5, 2020 01:35
-
-
Save mdmunir/ccc83af654f8644c8a602a619db5abd1 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 | |
| namespace app\controllers; | |
| use app\classes\SSE; | |
| use app\helpers\Notification; | |
| use Yii; | |
| use yii\web\Controller; | |
| /** | |
| * Description of EventController | |
| * | |
| * @author Misbahul D Munir <[email protected]> | |
| * @since 1.0 | |
| */ | |
| class EventController extends Controller | |
| { | |
| public function actionIndex() | |
| { | |
| $sse = new SSE(); | |
| $checkTime = Yii::$app->request->headers->get('last-event-id', 0); | |
| $userId = Yii::$app->user->id; | |
| $cx = mt_rand(5, 10); | |
| $sse->id(time()); | |
| while (true) { | |
| $data = Notification::getNotif($userId, $checkTime); | |
| if (count($data)) { | |
| $sse->event('notify', $data); | |
| } | |
| // kirimkan id | |
| $cx--; | |
| if ($cx <= 0) { | |
| $cx = mt_rand(5, 10); | |
| $sse->id(time()); | |
| $sse->event('test', ['a'=>'oke']); | |
| } | |
| $checkTime = time(); | |
| $sse->flush(); | |
| sleep(1); | |
| } | |
| exit(); | |
| } | |
| } |
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 | |
| namespace app\classes; | |
| /** | |
| * Description of SSE | |
| * | |
| * @author Misbahul D Munir <[email protected]> | |
| * @since 1.0 | |
| */ | |
| class SSE | |
| { | |
| /** | |
| * | |
| * @var boolean | |
| */ | |
| private $_headerSent; | |
| private function sendHeader() | |
| { | |
| if (!$this->_headerSent && !headers_sent()) { | |
| $this->_headerSent = true; | |
| header("Content-Type: text/event-stream"); | |
| header('Cache-Control: no-cache'); | |
| } | |
| } | |
| public function event($name, $data = null) | |
| { | |
| $this->sendHeader(); | |
| echo "event: {$name}\n"; | |
| if ($data !== null) { | |
| $this->sendData($data); | |
| } | |
| echo "\n"; | |
| } | |
| protected function sendData($data) | |
| { | |
| $messages = explode("\n", json_encode($data)); | |
| foreach ($messages as $message) { | |
| echo "data: {$message}\n"; | |
| } | |
| } | |
| public function message($data) | |
| { | |
| $this->sendHeader(); | |
| $this->sendData($data); | |
| echo "\n"; | |
| } | |
| public function id($id, $data = '') | |
| { | |
| $this->sendHeader(); | |
| echo "id: {$id}\n"; | |
| $this->sendData($data); | |
| echo "\n"; | |
| } | |
| public function retry($time) | |
| { | |
| $this->sendHeader(); | |
| echo "retry: {$time}\n\n"; | |
| } | |
| public function flush() | |
| { | |
| $this->sendHeader(); | |
| while (ob_get_level() > 0) { | |
| ob_end_flush(); | |
| } | |
| flush(); | |
| // break the loop if the client aborted the connection (closed the page) | |
| if (connection_aborted()) { | |
| exit(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment