public function flush()
{
if ($this->pollMode === null) {
// first polling request is not there yet
return;
}
if ($this->flushing) {
return;
}
$bsize = sizeof($this->buffer);
$fbsize = sizeof($this->framesBuffer);
if ($bsize === 0 && $fbsize === 0) {
return;
}
$this->flushing = true;
if (in_array('one-by-one', $this->pollMode)) {
$b = array_slice($this->buffer, 0, 1);
$bsize = sizeof($b);
} else {
$b = $this->buffer;
}
if ($fbsize > 0) {
if (!in_array('one-by-one', $this->pollMode) || !sizeof($b)) {
$b[] = 'a' . $this->toJson($this->framesBuffer);
} else {
$fbsize = 0;
}
}
$this->appInstance->publish('s2c:' . $this->id, $this->toJson($b), function ($redis) use($bsize, $fbsize, $b) {
$this->flushing = false;
if (!$redis) {
return;
}
//D(['b' => $b, $redis->result]);
if ($redis->result === 0) {
return;
}
$reflush = false;
if (sizeof($this->buffer) > $bsize) {
$this->buffer = array_slice($this->buffer, $bsize);
$reflush = true;
} else {
$this->buffer = [];
}
if (sizeof($this->framesBuffer) > $fbsize) {
$this->framesBuffer = array_slice($this->framesBuffer, $fbsize);
$reflush = true;
} else {
$this->framesBuffer = [];
}
$this->onWrite();
if ($reflush && in_array('stream', $this->pollMode)) {
$this->flush();
}
});
}