/** * @param $topic * @param $type * @param $msg * @throws \Exception * @return bool */ public function publish($topic, $type, $msg) { if (!$this->connected) { $this->connect(); } if (!$this->greeted) { $this->greet(); } if (!$this->jsonEnable) { $msgEntity = new \StringMessage(); $msgHeader = new \Header(); $msgHeader->set_groupId($this->group); $msgHeader->set_commit(true); $msgHeader->set_deliverLimit(100); $msgHeader->set_expiredTime(-1); $msgHeader->set_fly(false); $msgHeader->set_topic($topic); $guid = new Guid(); $msgHeader->set_messageId($guid->toString()); $msgHeader->set_messageType($type); $msgEntity->set_header($msgHeader); $msgEntity->set_body($msg); $send = $msgEntity->SerializeToString(); if (function_exists('kiteq_request')) { list($data, $type) = kiteq_request($this->conn, $send, self::CMD_STRING_MESSAGE); } else { // 发送 $this->innerSend($send, self::CMD_STRING_MESSAGE); // 接受 list($type, $data) = $this->innerGet(); } if ($type != self::CMD_MESSAGE_STORE_ACK) { throw new \Exception("kiteq 验证错误 MessageType {$type}"); } $ack = new \MessageStoreAck(); $ack->ParseFromString($data); if ($ack->getStatus() == false) { throw new \Exception($ack->getFeedback()); } return $ack->getStatus(); } else { $msgEntity = array(); $msgHeader = array('groupId' => $this->group, 'commit' => true, 'deliverLimit' => 100, 'expiredTime' => -1, 'fly' => false, 'topic' => $topic, 'messageId' => uniqid(), 'messageType' => $type); $msgEntity['header'] = $msgHeader; $msgEntity['body'] = $msg; $send = json_encode($msgEntity); if (function_exists('kiteq_request')) { list($data, $type) = kiteq_request($this->conn, $send, self::CMD_STRING_MESSAGE | 0x80); } else { // 发送 $this->innerSend($send, self::CMD_STRING_MESSAGE | 0x80); // 接受 list($type, $data) = $this->innerGet(); } if ($type != self::CMD_MESSAGE_STORE_ACK) { throw new \Exception("kiteq 验证错误 MessageType {$type}"); } $ack = json_decode($data, true); if ($ack->getStatus() == false) { throw new \Exception($ack->getFeedback()); } return $ack->getStatus(); } }