额,下面代码 客户端只能收到 $emptyEvents 数据,一直收不到 $endpointEvents 的数据,如果把if 和 else 的代码对调,那么客户端能收到$endpointEvents 但是收不到空包$emptyEvents数据
这是为何
$id = Timer::add(1, function () use ($connection, &$id, $clientId) {
// 连接关闭时,清除定时器
if ($connection->getStatus() !== TcpConnection::STATUS_ESTABLISHED) {
dump("init关闭连接");
Timer::del($id);
}
if (empty(Mcp::$connections[$clientId])) {
Mcp::$connections[$clientId] = $connection;
$endpointEvents = new ServerSentEvents(['event' => 'endpoint', 'data' => '/mcp/message?clientId=' . $clientId, 'id' => 1]);
dump("-------sse-------$clientId----------------");
$connection->send($endpointEvents);
} else {
$emptyEvents = new ServerSentEvents([]);
$connection->send($emptyEvents);
}
});