Merge branch 'master' of http://git.zhenyangtang.com.cn/ZYT/zyt
This commit is contained in:
@@ -9,8 +9,12 @@ namespace app\adminapi\service;
|
||||
*/
|
||||
final class AssistantSseProtocol
|
||||
{
|
||||
private const HEARTBEAT_INTERVAL_NS = 10_000_000_000;
|
||||
|
||||
private int $seq = 0;
|
||||
|
||||
private int $lastOutputAtNs = 0;
|
||||
|
||||
private bool $started = false;
|
||||
|
||||
private bool $terminal = false;
|
||||
@@ -36,12 +40,23 @@ final class AssistantSseProtocol
|
||||
|
||||
$this->seq = $nextSeq;
|
||||
$this->started = true;
|
||||
$this->lastOutputAtNs = hrtime(true);
|
||||
if (in_array($event, ['done', 'error'], true)) {
|
||||
$this->terminal = true;
|
||||
}
|
||||
return 'event: ' . $event . "\n" . 'data: ' . $encoded . "\n\n";
|
||||
}
|
||||
|
||||
/** Keep an idle authenticated stream open without inventing application events. */
|
||||
public function heartbeat(): ?string
|
||||
{
|
||||
if (!$this->started || $this->terminal) return null;
|
||||
$now = hrtime(true);
|
||||
if ($now - $this->lastOutputAtNs < self::HEARTBEAT_INTERVAL_NS) return null;
|
||||
$this->lastOutputAtNs = $now;
|
||||
return ": keep-alive\n\n";
|
||||
}
|
||||
|
||||
public function isTerminal(): bool
|
||||
{
|
||||
return $this->terminal;
|
||||
|
||||
Reference in New Issue
Block a user