-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
128 additions
and
7 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
vendor/ | ||
.idea/ | ||
composer.lock | ||
composer.lock | ||
examples/*.log |
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
include '../vendor/autoload.php'; | ||
|
||
use Hhxsv5\SSE\SSESwoole; | ||
use Hhxsv5\SSE\Update; | ||
|
||
// Swoole SSE Example: push messages to client | ||
|
||
$server = new Swoole\Http\Server('0.0.0.0', 5200); | ||
$server->set([ | ||
'enable_coroutine' => true, | ||
'max_coroutine' => 10000, // worker_num*10000 | ||
'reactor_num' => swoole_cpu_num() * 2, | ||
'worker_num' => swoole_cpu_num() * 2, | ||
'max_request' => 100000, | ||
'buffer_output_size' => 4 * 1024 * 1024, // 4MB | ||
'log_level' => SWOOLE_LOG_WARNING, | ||
'log_file' => __DIR__ . '/swoole.log', | ||
]); | ||
$server->on('Request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($server) { | ||
// $response->header('Access-Control-Allow-Origin', '*'); | ||
$response->header('Content-Type', 'text/event-stream'); | ||
$response->header('Cache-Control', 'no-cache'); | ||
$response->header('Connection', 'keep-alive'); | ||
$response->header('X-Accel-Buffering', 'no'); | ||
|
||
(new SSESwoole($request, $response))->start(new Update(function () { | ||
$id = mt_rand(1, 1000); | ||
$news = [['id' => $id, 'title' => 'title ' . $id, 'content' => 'content ' . $id]]; // Get news from database or service. | ||
if (empty($news)) { | ||
return false; // Return false if no new messages | ||
} | ||
return json_encode(compact('news')); | ||
}), 'news'); | ||
}); | ||
$server->start(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Hhxsv5\SSE; | ||
|
||
use Swoole\Coroutine; | ||
use Swoole\Http\Request; | ||
use Swoole\Http\Response; | ||
|
||
class SSESwoole | ||
{ | ||
protected $request; | ||
protected $response; | ||
|
||
public function __construct(Request $request, Response $response) | ||
{ | ||
$this->request = $request; | ||
$this->response = $response; | ||
} | ||
|
||
public function start(Update $update, $eventType = null, $milliRetry = 2000) | ||
{ | ||
while (true) { | ||
$changedData = $update->getUpdatedData(); | ||
if ($changedData !== false) { | ||
$event = [ | ||
'id' => uniqid('', true), | ||
'type' => $eventType, | ||
'data' => (string)$changedData, | ||
'retry' => $milliRetry, | ||
]; | ||
} else { | ||
$event = [ | ||
'comment' => 'no update', | ||
]; | ||
} | ||
$success = $this->response->write(new Event($event)); | ||
if (!$success) { | ||
$this->response->end(); | ||
return; | ||
} | ||
Coroutine::sleep($update->getCheckInterval()); | ||
} | ||
} | ||
} |