public function processRegister(Session $session, RegisterMessage $msg)
{
$registration = Registration::createRegistrationFromRegisterMessage($session, $msg);
if (count($this->registrations) > 0) {
// we already have something registered
if ($this->getAllowMultipleRegistrations()) {
return $this->addRegistration($registration, $msg);
} else {
// we are not allowed multiple registrations, but we may want
// to replace an orphaned session
$errorMsg = ErrorMessage::createErrorMessageFromMessage($msg, 'wamp.error.procedure_already_exists');
$options = $msg->getOptions();
// get the existing registration
/** @var Registration $oldRegistration */
$oldRegistration = $this->registrations[0];
if (isset($options->replace_orphaned_session) && $options->replace_orphaned_session == "yes") {
try {
$oldRegistration->getSession()->ping(5)->then(function ($res) use($session, $errorMsg) {
// the ping came back - send procedure_already_exists
$session->sendMessage($errorMsg);
}, function ($r) use($oldRegistration, $session, $registration, $msg) {
// bring down the exiting session because the
// ping timed out
$deadSession = $oldRegistration->getSession();
// this should do all the cleanup needed and remove the
// registration from this procedure also
$deadSession->shutdown();
// complete this registration now
return $this->addRegistration($registration, $msg);
});
} catch (\Exception $e) {
$session->sendMessage($errorMsg);
}
} else {
$session->sendMessage($errorMsg);
}
}
} else {
// this is the first registration
// setup the procedure to match the options
$this->setDiscloseCaller($registration->getDiscloseCaller());
$this->setAllowMultipleRegistrations($registration->getAllowMultipleRegistrations());
$this->setInvokeType($registration->getInvokeType());
return $this->addRegistration($registration, $msg);
}
}