simps / mqtt

🕹 MQTT Protocol Analysis and Coroutine Client for PHP. Support for 3.1, 3.1.1 and 5.0 versions of the MQTT protocol.

Home Page:https://mqtt.simps.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[QUESTION] 关于协程客户端

Dmcz opened this issue · comments

commented

SWOOLE不能有多个协程对一个 客户端 进行读 / 写操作。所以将读和写分成了两个协程, 通过channel来做通讯。

但这样似乎对mqtt ack 的处理不太友好。

想问问大佬们是咋处理的。

commented

现在的就是一一对应的,没使用channel。

commented

那也就会导致,监听消息之后。 没法再订阅,发布等操作了。

commented

这看你代码怎么写了

commented
\Hyperf\Utils\Coroutine::create(function() use($timeSincePing) {
    try {
        $client = $this->client();

        while($this->running){
            $buffer = $client->recv();

            if ($buffer && $buffer !== true) {
                $timeSincePing = time();

                $this->onMessage($buffer);
            }

            if ($timeSincePing <= (time() - $client->getConfig()->getKeepAlive())) {
                $buffer = $client->ping();
                if ($buffer) {
                    $this->write('ping success');
                    $timeSincePing = time();
                } else {
                    $client->close();
                    break;
                }
            }

        }

    } catch (\Throwable $th) {
        $this->write('recv coroutine has error: ' . $th->getMessage());
    }
    
});

我目前只是再协程中 while true 简单的实现了一下。
如果大佬有时间 看能不能 提供个例子参考。

commented

你用channel也行的,看下这两个吧 hyperf/hyperf#3260 hyperf/hyperf#3026

commented

ok, thx.