| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- <?php
- /**
- * This file is part of workerman.
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the MIT-LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @author walkor<walkor@workerman.net>
- * @copyright walkor<walkor@workerman.net>
- * @link http://www.workerman.net/
- * @license http://www.opensource.org/licenses/mit-license.php MIT License
- */
- declare(strict_types=1);
- namespace Workerman\Protocols\Http;
- use function str_replace;
- /**
- * Class ServerSentEvents
- * @package Workerman\Protocols\Http
- */
- class ServerSentEvents
- {
- /**
- * Data.
- * @var array
- */
- protected array $data;
- /**
- * ServerSentEvents constructor.
- * $data for example ['event'=>'ping', 'data' => 'some thing', 'id' => 1000, 'retry' => 5000]
- * @param array $data
- */
- public function __construct(array $data)
- {
- $this->data = $data;
- }
- /**
- * __toString.
- *
- * @return string
- */
- public function __toString()
- {
- $buffer = '';
- $data = $this->data;
- if (isset($data[''])) {
- $buffer = ": {$data['']}\n";
- }
- if (isset($data['event'])) {
- $buffer .= "event: {$data['event']}\n";
- }
- if (isset($data['id'])) {
- $buffer .= "id: {$data['id']}\n";
- }
- if (isset($data['retry'])) {
- $buffer .= "retry: {$data['retry']}\n";
- }
- if (isset($data['data'])) {
- $buffer .= 'data: ' . str_replace("\n", "\ndata: ", $data['data']) . "\n";
- }
- return $buffer . "\n";
- }
- }
|