Exemplo n.º 1
0
 public function testMessageConstructorWithTraversableObjects()
 {
     $body = new \ArrayObject();
     $body['content'] = 'Hello';
     $message = new Message($body, $body);
     $this->assertSame(['content' => 'Hello'], $message->getData());
     $this->assertSame(['content' => 'Hello'], $message->getMetadata());
 }
Exemplo n.º 2
0
 public function testSettersAreChainable()
 {
     $message = new Message();
     $message->collapseKey('collapse-key')->delayWhileIdle(true)->dryRun(true)->timeToLive(100)->data(array('key1' => 'value1'))->addData('key2', 'value2')->restrictedPackageName('com.lukekorth.android');
     $this->assertEquals('collapse-key', $message->getCollapseKey());
     $this->assertEquals(true, $message->getDelayWhileIdle());
     $this->assertEquals(true, $message->getDryRun());
     $this->assertEquals(100, $message->getTimeToLive());
     $this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $message->getData());
     $this->assertEquals('com.lukekorth.android', $message->getRestrictedPackageName());
 }
Exemplo n.º 3
0
 private function validateData(Message $message)
 {
     $notice = $message->getNotification();
     if (strlen($notice) > 2048) {
         throw new Exception('Notification payload is to big (max 2048)', Exception::MALFORMED_REQUEST);
         return false;
     }
     $data = $message->getData();
     if (strlen(json_encode($data)) > 4096) {
         throw new Exception('Data payload is to big (max 4096)', Exception::MALFORMED_REQUEST);
         return false;
     }
     return true;
 }
Exemplo n.º 4
0
 /**
  * Sends a message to a message queue.
  *
  * @param Message $message   A Message instance
  * @param bool    $serialize If true, $message will be serialized
  * @param bool    $blocking  If true, the process will be blocked until another process reads messages
  *                           from the queue and frees enough space for your message to be sent,
  *                           otherwise will be thrown OverflowException
  *
  * @throws \OverflowException When a message queue is overwoled
  * @throws \RuntimeException  When could not send a $message
  */
 public function send(Message $message, $serialize = true, $blocking = true)
 {
     $this->checkState();
     if (!@msg_send($this->handle, $message->getType(), $message->getData(), $serialize, $blocking, $errCode)) {
         if ($errCode === MSG_EAGAIN) {
             throw new \OverflowException(sprintf('Overflow a message queue with key "%s", not enough the space for a new message.', $this->key));
         }
         $detail = null;
         if ($errCode === 22) {
             $detail = 'Maybe the message size is too big.';
         }
         throw new \RuntimeException(sprintf('Could not send the message to a message queue with key "%s" (error code: %d). %s', $this->key, $errCode, $detail));
     }
 }
Exemplo n.º 5
0
 /**
  * Sends a message without retrying in case of service unavailability. See
  * sendMulti() for more info.
  *
  * @return {@literal true} if the message was sent successfully,
  *         {@literal false} if it failed but could be retried.
  *
  * @throws \InvalidArgumentException if registrationIds is {@literal null} or
  *         empty.
  * @throws InvalidRequestException if GCM didn't returned a 200 status.
  * @throws \Exception if message could not be sent or received.
  */
 public function sendNoRetryMulti(Message $message, array $registrationIds)
 {
     if (is_null($registrationIds) || count($registrationIds) == 0) {
         throw new \InvalidArgumentException('registrationIds cannot be null or empty');
     }
     $request = array();
     if ($message->getTimeToLive() != -1) {
         $request[Constants::$PARAM_TIME_TO_LIVE] = $message->getTimeToLive();
     }
     if ($message->getCollapseKey() != '') {
         $request[Constants::$PARAM_COLLAPSE_KEY] = $message->getCollapseKey();
     }
     if ($message->getDelayWhileIdle() != '') {
         $request[Constants::$PARAM_DELAY_WHILE_IDLE] = $message->getDelayWhileIdle();
     }
     $request[Constants::$JSON_REGISTRATION_IDS] = $registrationIds;
     if (!is_null($message->getData()) && count($message->getData()) > 0) {
         $request[Constants::$JSON_PAYLOAD] = $message->getData();
     }
     $request = json_encode($request);
     $headers = array('Content-Type: application/json', 'Authorization: key=' . $this->key);
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, Constants::$GCM_SEND_ENDPOINT);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
     $response = curl_exec($ch);
     $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     curl_close($ch);
     if ($status != 200) {
         throw new InvalidRequestException($status, $response);
     }
     $response = json_decode($response, true);
     $success = $response[Constants::$JSON_SUCCESS];
     $failure = $response[Constants::$JSON_FAILURE];
     $canonicalIds = $response[Constants::$JSON_CANONICAL_IDS];
     $multicastId = $response[Constants::$JSON_MULTICAST_ID];
     $multicastResult = new MulticastResult($success, $failure, $canonicalIds, $multicastId);
     if (isset($response[Constants::$JSON_RESULTS])) {
         $individualResults = $response[Constants::$JSON_RESULTS];
         foreach ($individualResults as $singleResult) {
             $messageId = isset($singleResult[Constants::$JSON_MESSAGE_ID]) ? $singleResult[Constants::$JSON_MESSAGE_ID] : null;
             $canonicalRegId = isset($singleResult[Constants::$TOKEN_CANONICAL_REG_ID]) ? $singleResult[Constants::$TOKEN_CANONICAL_REG_ID] : null;
             $error = isset($singleResult[Constants::$JSON_ERROR]) ? $singleResult[Constants::$JSON_ERROR] : null;
             $result = new Result();
             $result->setMessageId($messageId);
             $result->setCanonicalRegistrationId($canonicalRegId);
             $result->setErrorCode($error);
             $multicastResult->addResult($result);
         }
     }
     return $multicastResult;
 }
Exemplo n.º 6
0
 public function testSetsDataCorrectly()
 {
     $message = new Message();
     $message->data(array('key1' => 'value1', 'key2' => 'value2'));
     $this->assertEquals(array('key1' => 'value1', 'key2' => 'value2'), $message->getData());
 }
Exemplo n.º 7
0
 /**
  * Send data
  * @param array $data
  * @return boolean
  */
 public function send(Message $message)
 {
     $router = $this->_params['router'];
     if (!$router) {
         $this->_logger->warn('Publish router is not defined: message cannot be sent');
         return;
     }
     //创建channel
     $channel = $this->_getChannel();
     //创建exchange
     $exchange = $this->_getExchange();
     if (!$exchange->declareExchange()) {
         return false;
     }
     //创建队列
     $this->_getQueue();
     $publishParams = ['content_type' => 'text/plain', 'app_id' => 100, 'message_id' => 100, 'delivery_mode' => 2, 'priority' => 9, 'type' => 'system', 'timestamp' => time(), 'reply_to' => 'hacker', 'headers' => ['name' => 'Musikar']];
     $publishParams = array_merge($this->_publishParams, $publishParams);
     $channel->startTransaction();
     //将你的消息通过制定routingKey发送
     $data = $message->getData();
     foreach ($data as $value) {
         $result = $exchange->publish(serialize($value), $router, AMQP_NOPARAM, $publishParams);
     }
     $channel->commitTransaction();
     $this->close();
     return $result;
 }
Exemplo n.º 8
0
 /**
  * Send message action
  *
  * @param Message $message
  * @return mixed
  */
 public function send(Message $message)
 {
     return $this->call('imbot.message.add', $message->getData(), $this->auth);
 }