首页 » Web前端 » phpmsg技巧_phpmqtt之运用类方法作为procmsg回调函数

phpmsg技巧_phpmqtt之运用类方法作为procmsg回调函数

访客 2024-11-12 0

扫一扫用手机浏览

文章目录 [+]

在物联网项目里,phpmqtt这个库是目前险些最常见、利用最广泛的一个mqtt通讯类库。
大部分涉及到mqtt通讯的项目险些都会利用到它。
利用该库来构建物联网设备的运用做事端,流程非常清晰:

1、订阅主题的同时,设置利益置函数;

phpmsg技巧_phpmqtt之运用类方法作为procmsg回调函数

2、去世循环侦听处理。

phpmsg技巧_phpmqtt之运用类方法作为procmsg回调函数
(图片来自网络侵删)

下面这段程序网上到处可见:

<?phprequire("../phpMQTT.php");$server = '127.0.0.1'; // 做事器IP$port = 1883; // 做事器端口$username = ''; // 用户名$password = ''; // 密码$client_id = 'clientid';$mqtt = new phpMQTT($server, $port, $client_id);$mqtt->debug = true;if(!$mqtt->connect(true, NULL, $username, $password)){ echo "连接失落败!\n"; exit(1);}// 订阅列表$topics = [ 'topic01' => ['qos' => 0, 'function' => 'procmsg'],];$mqtt->subscribe($topics, 0);while ($mqtt->proc()){}$mqtt->close();// 处理函数function procmsg($topic, $msg){ echo date('Y-m-d H:i:s') . ' [' . $topic . '] ' . $msg . PHP_EOL;}

这段代码用了最直不雅观的方法写成,代码里没有任何类的封装,表示了高度朴素的唯物主义精神。
奇怪的是,网上能搜到的代码,险些都是类似上述的一段或者多段,看来猿猿们都非常长于和乐于节约笔墨,高度尊重原创者的版权,恨不能一字不改都用上。
于是,用类的方法来布局上述过程的例子,一个都没看到。
于是问题来了,如果上述代码中的procmsg,是一个类的方法的话,topics数组里的'function' => 'procmsg',到底该当若何书写才精确呢?

换句话说,procmsg处理函数,能否利用类的方法来实现?

当然能。
——那么,怎么写呢?

相信有很多人考试测验过把'function' => 'procmsg'写成:'function' => 'self::procmsg'、'function' => '$this->procmsg',然后无一例外地创造回调没有发生。
这是为什么?

首先,procmsg显然是函数名称,而且,显然是一个被mqtt回调的函数名称。
有了这个线索就好办了,让我们扒开phpmqtt的代码看看,案创造场到底是什么。
于是我们在phpmqtt.php里看到了这段代码:

/ message: processes a recieved topic / function message($msg){ $tlen = (ord($msg{0})<<8) + ord($msg{1}); $topic = substr($msg,2,$tlen); $msg = substr($msg,($tlen+2)); $found = 0; foreach($this->topics as $key=>$top){ if( preg_match("/^".str_replace("#",".", str_replace("+","[^\/]", str_replace("/","\/", str_replace("$",'\$', $key))))."$/",$topic) ){ if(is_callable($top['function'])){ call_user_func($top['function'],$topic,$msg); $found = 1; } } }

把稳第13行和14行,两个熟习的身影涌现了:

is_callablecall_user_func

不理解上述函数的,请自行度娘。

找到call_user_func的解释和示例:「链接」

Example #4 Using a class method with call_user_func()

<?phpclass myclass {static function say_hello(){echo "Hello!\n";}}$classname = "myclass";call_user_func(array($classname, 'say_hello'));call_user_func($classname .'::say_hello');$myobject = new myclass();call_user_func(array($myobject, 'say_hello'));?>

可见,代码中的$top['function'],也便是'function' => 'procmsg'中的'procmsg',可以利用array($obj,'method_name')来表示。

以是,上述代码

// 订阅列表$topics = [ 'topic01' => ['qos' => 0, 'function' => 'procmsg'],];

就可以写成下面这样:

class example{ public function listen(){xxxxxx; // 省略其他代码// 订阅列表$procfunc = array($this, "procmsg");$topics = [ 'topic01' => ['qos' => 0, 'function' => $procfunc],];$mqtt->subscribe($topics, 0);while ($mqtt->proc()){}$mqtt->close();} public function procmsg($topic, $msg){ echo "hello\n"; echo "Msg Recieved: " . date("r") . " Topic: {$topic}\n"; echo "\t$msg\n"; }}

上述方法,利用的是类的实例。
当然也可以利用静态类、静态方法。
详细魄式可以参考「链接」。

标签:

相关文章

介绍百度码,技术革新背后的智慧之光

随着科技的飞速发展,互联网技术已经成为我们生活中不可或缺的一部分。而在这个信息爆炸的时代,如何快速、准确地获取信息,成为了人们关注...

Web前端 2025-01-03 阅读1 评论0

介绍皮箱密码,开启神秘之门的钥匙

皮箱,作为日常生活中常见的收纳工具,承载着我们的珍贵物品。面对紧闭的皮箱,许多人却束手无策。如何才能轻松打开皮箱呢?本文将为您揭秘...

Web前端 2025-01-03 阅读0 评论0

介绍盗号器,网络安全的隐忧与应对步骤

随着互联网的快速发展,网络安全问题日益突出。盗号器作为一种非法工具,对网民的个人信息安全构成了严重威胁。本文将深入剖析盗号器的原理...

Web前端 2025-01-03 阅读1 评论0