public function send($clientId, string $data, bool $binary = false) : Promise
{
if ($clientId === null) {
$clientId = array_keys($this->clients);
}
if (\is_array($clientId)) {
$promises = [];
foreach ($clientId as $id) {
$promises[] = $this->send($id, $data, $binary);
}
return all($promises);
}
if ($client = $this->clients[$clientId] ?? null) {
$client->messagesSent++;
$opcode = $binary ? self::OP_BIN : self::OP_TEXT;
assert($binary || preg_match("//u", $data), "non-binary data needs to be UTF-8 compatible");
if (\strlen($data) > 1.5 * $this->autoFrameSize) {
$len = \strlen($data);
$slices = ceil($len / $this->autoFrameSize);
$frames = str_split($data, ceil($len / $slices));
$data = array_pop($frames);
foreach ($frames as $frame) {
$this->compile($client, $frame, $opcode, false);
$opcode = self::OP_CONT;
}
}
return $this->compile($client, $data, $opcode);
}
return new Success();
}