walkor / mqtt

Asynchronous MQTT client for PHP based on workerman.
207 stars 46 forks source link

How to disconnect inside a Chat client? #7

Open rcpinheiro opened 4 years ago

rcpinheiro commented 4 years ago

I've altered the chatroom example to let the user exit the chatroom:

        $console = new TcpConnection(STDIN);
        $console->onMessage = function($console, $message) use ($mqtt){
            $message=trim($message);
            if ($message=="exit") {
                $mqtt->disconnect();
                die;
            } else {
                $mqtt->publish('testRoom', $message);
            }
        };

When the user enters "exit" I want the worker to stop but it reconnects always. I've tried using "$mqtt->doNotReconnect=true;" but it doesn't work. Anyone knows how to do this?

mouyong commented 2 years ago

Maybe you can use $mqtt->close() to close connection. @rcpinheiro