最近看了看nsq的协议 发现比较简单,搜了一下php的客户端 要么是扩展、要么需要swoole、要么很复杂。于是自己撸了一个 支持php5.6+ 仓库地址:https://github.com/lizhichao/one-nsq
composer require lizhichao/one-nsq
$ct = new \OneNsq\Client('tcp://127.0.0.1:4150');
//$ct->auth('12345');
// subscribe
$res = $ct->subscribe('test', 's2');
// $ct2 = new \OneNsq\Client('tcp://192.168.23.129:4150');
// $res2 = $ct2->subscribe('test2', 's1');
foreach ($res as $data)
{
// $data = $res2->current();
// $res2->next();
if ($data === null) {
echo 'null' . PHP_EOL;
echo "\n --------------- \n";
continue;
}
echo 'attempts:' . $data->attempts . PHP_EOL;
echo 'msg:' . $data->msg . PHP_EOL;
echo 'time:' . date('Y-m-d H:i:s', $data->timestamp) . PHP_EOL;
echo "\n --------------- \n";
// $ct->touch($data->id);
};
// publish
for ($i = 0; $i < 6; $i++) {
$ct->publish('test', 'msg:' . $i . ' time:' . date('Y-m-d H:i:s'));
}
for ($i = 0; $i < 3; $i++) {
$ct->publishMany('test', [
'm-msg:' . $i . '-1 time:' . date('Y-m-d H:i:s'),
'm-msg:' . $i . '-2 time:' . date('Y-m-d H:i:s'),
'm-msg:' . $i . '-3 time:' . date('Y-m-d H:i:s')
]);
}
for ($i = 0; $i < 6; $i++) {
$ct->publish('test', 'd-msg:' . $i . ' time:' . date('Y-m-d H:i:s'), ($i + 1) * 1000);
}
订阅和其他库不一样 没用用闭包 这里使用的是迭代器。迭代器可以支持同监听多个