/** * Completes a initiated association with a message from the second device. * This device is requesting for association with the first one. * If the two facebook IDs (or codes) matches, then the association is * successfully completed. */ public function completeWithMessage(ConnectionInterface $from, Message $message) { if ($message->getName() == MessageTypes::ASSOCIATE_WITH_FACEBOOK) { $givenFacebookId = $message->get('facebook_id'); if ($givenFacebookId == $this->getFacebookId()) { $this->setCompleted(true); } } elseif ($message->getName() == MessageTypes::ASSOCIATE_WITH_CODE) { $givenCode = $message->get('code'); if ($givenCode == $this->getAssociationCode()) { $this->setCompleted(true); } } if ($this->getCompleted()) { $this->setMobileSocket($from); echo "Now associated with client {$from->resourceId}\n"; return true; } else { return false; } }
/** * Process a message. * If the two devices haven' been associated yet, * this is where the association process takes place. * Otherwise, the message is simply dispatched to the devices association, * which in turns takes care of delivering the message to the device. */ protected function processMessage(ConnectionInterface $from, $message) { $messageData = json_decode($message, true); // create the message object from the received data $message = Message::fromRawData($messageData); if ($message->getName() == MessageTypes::BEGIN_FACEBOOK_ASSOCIATION || $message->getName() == MessageTypes::BEGIN_CODE_ASSOCIATION) { // If it is an association initialization, // we temporarely register the incomplete association, // waiting for the second device to associate. $association = Association::createFromMessage($from, $message); $identifier = $association->getFacebookId() ?: $association->getAssociationCode(); $this->ongoingAssociations[$identifier] = $association; } elseif ($message->getName() == MessageTypes::ASSOCIATE_WITH_FACEBOOK || $message->getName() == MessageTypes::ASSOCIATE_WITH_CODE) { // Now we have a request for association from the second device. // We rely on the Association::completeWithMessage method to // validate the association (with right code/facebook ID) and if so, // the association is registered as complete and the two devices can now // communicate. $identifier = $message->get('facebook_id') ?: $message->get('code'); if (!array_key_exists($identifier, $this->ongoingAssociations)) { $from->send(json_encode(array('direction' => MessageTypes::FROM_PLAYER_TO_DEVICE, 'name' => MessageTypes::ASSOCIATION_REFUSED))); return; } $association = $this->ongoingAssociations[$identifier]; if ($association->completeWithMessage($from, $message)) { $this->registerAssociation($association); } else { $from->send(json_encode(array('direction' => MessageTypes::FROM_PLAYER_TO_DEVICE, 'name' => MessageTypes::ASSOCIATION_REFUSED))); } } else { // For all other messages, a token should be provided // If so, we should have at this point a TokenizedMessage instance // (provided by the Message::create factory method). // Otherwise it means that no token has been provided, and we // cannot do anything else without the token. if (!$message instanceof TokenizedMessage) { $from->send(json_encode(array('code' => 400, 'message' => 'No token information was provided.'))); return; } $this->completedAssociations[$message->getToken()]->dispatch($message); } }