public function handleRouterStart(RouterStartEvent $event) { $server = new Server($this->bindAddress, $this->port, false, ["wamp.2.json"]); Logger::info($this, "Websocket listening on " . $this->bindAddress . ":" . $this->port); $this->serverDisposable = $server->subscribe(new CallbackObserver(function (MessageSubject $ms) { $this->createNewSessionForMessageSubject($ms); }, function (\Exception $err) { Logger::error($this, "Received error on server: " . $err->getMessage()); }, function () { Logger::alert($this, "Completed. Not sure if we should ever do that."); })); }
/** * @param Subscription $subscription * @return StateHandlerRegistration|bool|null */ private function getStateHandlerRegistrationForSubscription(Subscription $subscription) { $subscriptionGroup = $subscription->getSubscriptionGroup(); if ($subscriptionGroup instanceof SubscriptionGroup) { if (!$this->stateHandlerMap->contains($subscriptionGroup)) { $this->setupStateHandlerRegistration($subscriptionGroup); } return $this->stateHandlerMap[$subscriptionGroup]; } Logger::alert($this, "processSubscriptionAdded called with subscription that does not have subscriptionGroup set."); return false; }
/** * @return int */ public function decPendingCallCount() { // if we are already at zero - something is wrong if ($this->pendingCallCount == 0) { Logger::alert($this, 'Session pending call count wants to go negative.'); return 0; } return $this->pendingCallCount--; }
/** * Process All Messages if the session has been authenticated * * @param \Thruway\Session $session * @param \Thruway\Message\Message $msg */ private function processAuthenticated(Session $session, Message $msg) { // authorization stuff here if ($msg instanceof ActionMessageInterface) { if (!$this->getAuthorizationManager()->isAuthorizedTo($session, $msg)) { Logger::alert($this, "Permission denied: " . $msg->getActionName() . " " . $msg->getUri() . " for " . $session->getAuthenticationDetails()->getAuthId()); $session->sendMessage(ErrorMessage::createErrorMessageFromMessage($msg, "wamp.error.not_authorized")); return; } } $handled = false; foreach ($this->roles as $role) { if ($role->handlesMessage($msg)) { $role->onMessage($session, $msg); $handled = true; break; } } if (!$handled) { Logger::warning($this, "Unhandled message sent to \"{$this->getRealmName()}\""); } }
/** @inheritdoc */ public function onMessage(ConnectionInterface $from, $msg) { Logger::debug($this, "onMessage: ({$msg})"); /** @var Session $session */ $session = $this->sessions[$from]; try { //$this->router->onMessage($transport, $transport->getSerializer()->deserialize($msg)); $msg = $session->getTransport()->getSerializer()->deserialize($msg); if ($msg instanceof HelloMessage) { $details = $msg->getDetails(); $details->transport = (object) $session->getTransport()->getTransportDetails(); $msg->setDetails($details); } $session->dispatchMessage($msg); } catch (DeserializationException $e) { Logger::alert($this, "Deserialization exception occurred."); } catch (\Exception $e) { Logger::alert($this, "Exception occurred during onMessage: " . $e->getMessage()); } }
/** * Triggered when a client sends data through the socket * * @param \Ratchet\ConnectionInterface $from The socket/connection that sent the message to your application * @param string $msg The message received * @throws \Exception */ public function onMessage(ConnectionInterface $from, $msg) { Logger::debug($this, "onMessage: ({$msg})"); /** @var TransportInterface $transport */ $transport = $this->transports[$from]; try { $this->peer->onMessage($transport, $transport->getSerializer()->deserialize($msg)); } catch (DeserializationException $e) { Logger::alert($this, "Deserialization exception occurred."); } catch (\Exception $e) { Logger::alert($this, "Exception occurred during onMessage: " . $e->getMessage()); } }
/** * Process subscribe message * * @param \Thruway\Session $session * @param \Thruway\Message\SubscribeMessage $msg * @throws \Exception */ protected function processSubscribe(Session $session, SubscribeMessage $msg) { // get a subscription group "hash" /** @var MatcherInterface $matcher */ $matcher = $this->getMatcherForMatchType($msg->getMatchType()); if ($matcher === null) { Logger::alert($this, "no matching match type for \"" . $msg->getMatchType() . "\" for URI \"" . $msg->getUri() . "\""); return; } if (!$matcher->uriIsValid($msg->getUri(), $msg->getOptions())) { $errorMsg = ErrorMessage::createErrorMessageFromMessage($msg); $session->sendMessage($errorMsg->setErrorURI('wamp.error.invalid_uri')); return; } $matchHash = $matcher->getMatchHash($msg->getUri(), $msg->getOptions()); if (!isset($this->subscriptionGroups[$matchHash])) { $this->subscriptionGroups[$matchHash] = new SubscriptionGroup($matcher, $msg->getUri(), $msg->getOptions()); } /** @var SubscriptionGroup $subscriptionGroup */ $subscriptionGroup = $this->subscriptionGroups[$matchHash]; $subscription = $subscriptionGroup->processSubscribe($session, $msg); $registry = $this->getStateHandlerRegistry(); if ($registry !== null) { $registry->processSubscriptionAdded($subscription); } }
/** * @param Session $session * @param UnsubscribeMessage $msg * @return bool|Subscription */ public function processUnsubscribe(Session $session, UnsubscribeMessage $msg) { if ($this->containsSubscriptionId($msg->getSubscriptionId())) { /** @var Subscription $subscription */ $subscription = $this->subscriptions[$msg->getSubscriptionId()]; if ($session !== $subscription->getSession()) { Logger::alert($this, "Unsubscribe request from non-owner: " . json_encode($msg)); return false; } $this->removeSubscription($subscription); $session->sendMessage(new UnsubscribedMessage($msg->getRequestId())); return $subscription; } return false; }