EMP 实现消息发布订阅
官网文档
http://emqtt.com/docs/v2/index.html
php 实现消息发布
使用第三方包
bluerhinos/phpmqtt
$mqtt = new phpMQTT(“localhost”, 1883, “clientId”);
//Change ClientId to something unique,此处必须唯一,一个客户端对应一个id
if ($mqtt->connect()) {for($i = 0; $i < 20; $i++) {
sleep(1);
// $mqtt->publish(‘/Worldd’,’Hello new ‘.$i.’laravel!’,0);
$mqtt->publish("World","Hello laravel! at ".date("r"),0);
}
$mqtt->close();
}
消息订阅
php实现
<?php
require dirname(DIR) . “/vendor/autoload.php”;
use Bluerhinos\phpMQTT;$server = “localhost”; // change if necessary
$port = 1883; // change if necessary
$username = “”; // set your username
$password = “”; // set your password
$client_id = “phh-subscriber”; // make sure this is unique for connecting to sever - you could use uniqid()
$micro_time = round(microtime(true) * 1000);
$client_id = $client_id . $micro_time;$mqtt = new phpMQTT($server, $port, $client_id);
if (!$mqtt->connect(true, NULL, $username, $password)) {
exit(1);
}
function procmsg($topic, $msg)
{echo "Msg Recieved: " . date("r") . "\n";
echo "Topic: { $topic}\n\n";
echo "\t$msg\n\n";
}
$topics[‘World’] = array(“qos” => 0, “function” => “procmsg”);
$mqtt->subscribe($topics, 0);while ($mqtt->proc()) {
}
$mqtt->close();
js websocket 订阅消息
还没有评论,来说两句吧...