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

[QUESTION] 关于协程客户端 #37

Closed
Dmcz opened this issue Feb 19, 2021 · 6 comments
Closed

[QUESTION] 关于协程客户端 #37

Dmcz opened this issue Feb 19, 2021 · 6 comments
Assignees
Labels
question Further information is requested

Comments

@Dmcz
Copy link

Dmcz commented Feb 19, 2021

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

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

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

@Dmcz Dmcz added the question Further information is requested label Feb 19, 2021
@sy-records
Copy link
Member

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

@Dmcz
Copy link
Author

Dmcz commented Feb 19, 2021

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

@sy-records
Copy link
Member

这看你代码怎么写了

@Dmcz
Copy link
Author

Dmcz commented Feb 19, 2021

\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 简单的实现了一下。
如果大佬有时间 看能不能 提供个例子参考。

@sy-records
Copy link
Member

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

@Dmcz
Copy link
Author

Dmcz commented Feb 20, 2021

ok, thx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants