示例#1
0
 /**
  * Sends asynchronous internal message to Node.js
  *  If "Q.clientId" is in $_REQUEST, adds it into the data
  * @method sendToNode
  * @static
  * @param {array} $data Associative array of data of the message to send.
  *  It should contain the key "Q/method" so Node can decide what to do with the message.
  * @param {string|array} [$url=null] and url to query. Default to 'Q/nodeInternal' config value and path '/Q/node'
  * @param {boolean} [$throwIfRefused=false] Pass true here to throw an exception whenever Node process is not running or refuses the request
  */
 static function sendToNode($data, $url = null, $throwIfRefused = false)
 {
     if (!is_array($data)) {
         throw new Q_Exception_WrongType(array('field' => 'data', 'type' => 'array'));
     }
     if (empty($data['Q/method'])) {
         throw new Q_Exception_RequiredField(array('field' => 'Q/method'));
     }
     $ssid = Q_Request::special('clientId', null);
     if (isset($ssid)) {
         $data['Q.clientId'] = $ssid;
     }
     // The following hook may modify the url
     /**
      * @event Q/Utils/sendToNode {before}
      * @param {array} data
      * @param {string|array} 'url'
      */
     Q::event('Q/Utils/sendToNode', array('data' => $data, 'url' => $url), 'before');
     if (!$url) {
         $nodeh = Q_Config::get('Q', 'nodeInternal', 'host', null);
         $nodep = Q_Config::get('Q', 'nodeInternal', 'port', null);
         $url = $nodep && $nodeh ? "http://{$nodeh}:{$nodep}/Q/node" : false;
     }
     if (!$url) {
         $result = false;
     } else {
         // Should we switch to sending JSON over TCP?
         $result = Q_Utils::postAsync($url, self::sign($data), null, Q_UTILS_INTERNAL_TIMEOUT, $throwIfRefused);
     }
     return $result;
     //		if (!$result) {
     //			throw new Q_Exception_SendingToNode(array('method' => $data['Q/method']));
     //		}
 }