public function pSubscribe($patterns, $callback)
{
if (!$this->standalone) {
return $this->__call('pSubscribe', array((array) $patterns, $callback));
}
// Standalone mode: use infinite loop to subscribe until timeout
$patternCount = is_array($patterns) ? count($patterns) : 1;
while ($patternCount--) {
if (isset($status)) {
list($command, $pattern, $status) = $this->read_reply();
} else {
list($command, $pattern, $status) = $this->__call('psubscribe', array($patterns));
}
$this->subscribed = $status > 0;
if (!$status) {
throw new CredisException('Invalid pSubscribe response.');
}
}
try {
while ($this->subscribed) {
list($type, $pattern, $channel, $message) = $this->read_reply();
if ($type != 'pmessage') {
throw new CredisException('Received non-pmessage reply.');
}
$callback($this, $pattern, $channel, $message);
}
} catch (CredisException $e) {
if ($e->getCode() == CredisException::CODE_TIMED_OUT) {
try {
list($command, $pattern, $status) = $this->pUnsubscribe($patterns);
while ($status !== 0) {
list($command, $pattern, $status) = $this->read_reply();
}
} catch (CredisException $e2) {
throw $e2;
}
}
throw $e;
}
}