Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement alias. #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions examples/alias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
//引入API库
require "../yunba.php";

//构造实例
$yunba = new Yunba(array(
"appkey" => "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();

?>
64 changes: 64 additions & 0 deletions yunba.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
}

/**
* 等待通讯
*/
Expand Down Expand Up @@ -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];
Expand Down