diff --git a/examples/alias.php b/examples/alias.php new file mode 100644 index 0000000..aa4ebca --- /dev/null +++ b/examples/alias.php @@ -0,0 +1,54 @@ + "535f78721a88e6c860569ac6" +)); + +//初始化 +$yunba->init(function ($success) { + echo "[YunBa]init " . ($success ? "success" : "fail") . "\n"; +}); + +//连接 +$yunba->connect(function ($success) { + if ($success) { + echo "[YunBa]connect success\n"; + } + else { + echo "[YunBa]connect fail\n"; + } +}); + +//等待连接完毕 +sleep(1); + +//设置别名alias1 +$yunba->set_alias(array( + "alias" => "alias1" +), function ($success) { + echo "[Yunba]set alias to alias1 " . ($success ? "success" : "fail") . "\n"; +}, function ($data) { + echo "[YunBa]received " . var_export($data, true) . "\n"; +}); + +//获取当前别名 +$yunba->get_alias(function($alias){ + echo "[YunBa]current alias is " . $alias . "\n"; +}); + + +//发布消息到alias1 +$yunba->publish_to_alias(array( + "alias" => "alias1", + "msg" => "Hello,World" +), function ($success) { + echo "[YunBa]publish to alias1 " . ($success ? "success" : "fail") . "\n"; +}); + +//等待通讯 +$yunba->wait(); + +?> \ No newline at end of file diff --git a/yunba.php b/yunba.php index cb67d43..f77cc1c 100644 --- a/yunba.php +++ b/yunba.php @@ -673,6 +673,8 @@ public function init($initCallback = null, $recCallback = null) { $this->on("message", array($this, "_messageCallbackMethod")); $this->on("puback", array($this, "_publishCallbackMethod")); $this->on("unsuback", array($this, "_unsubscribeCallbackMethod")); + $this->on("set_alias_ack", array($this, "_setAliasCallbackMethod")); + $this->on("alias", array($this, "_getAliasCallbackMethod")); $this->_client->init(); } @@ -776,6 +778,54 @@ public function publish (array $args, $callback = null) { ), $callback); } + /** + * 设置别名 + * + * @param array $args 参数,为 alias + * @param callable $setAliasCallback 设置结果回调 + * @param callable $messageCallback 消息接收回调 + */ + public function set_alias (array $args, $setAliasCallback = null, $messageCallback = null) { + $alias = isset($args["alias"]) ? $args["alias"] : ""; + + if (is_callable($messageCallback)) { + if (!isset($this->_messageCallbacks[$alias])) { + $this->_messageCallbacks[$alias] = array(); + } + $this->_messageCallbacks[$alias][] = $messageCallback; + } + + $this->emit("set_alias", array( + "alias" => $alias + ), $setAliasCallback); + } + + /** + * 获取别名 + * + * @param callable $callback 回调 + */ + public function get_alias ($callback = null) { + $this->emit("get_alias", array(), $callback); + } + + /** + * 向用户别名发送消息, 用于实现点对点的消息发送。 + * + * @param array $args 参数,包括 alias, msg 两个参数 + * @param callable $callback 回调 + */ + public function publish_to_alias (array $args, $callback = null) { + if (isset($args["alias"]) and isset($args["msg"])){ + $alias = $args["alias"]; + $msg = $args["msg"]; + $this->emit("publish_to_alias", array( + "alias" => $alias, + "msg" => $msg + ), $callback); + } + } + /** * 等待通讯 */ @@ -850,6 +900,20 @@ public function _unsubscribeCallbackMethod($data) { } } + public function _setAliasCallbackMethod($data) { + $callback = $this->_fetchCallback(); + if ($callback) { + call_user_func($callback, $data["success"]); + } + } + + public function _getAliasCallbackMethod($data) { + $callback = $this->_fetchCallback(); + if ($callback) { + call_user_func($callback, $data["alias"]); + } + } + public function __call($method, $args) { if (preg_match("/^push_callback_(\\d+)$/", $method, $match)) { $callId = $match[1];