Call stop()
on the IOServer
's loop.
Launcher.php
namespace MyApp;
use Ratchet\Server\IoServer;
use Ratchet\Http\HttpServer;
use MyApp\Server;
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Server('stopCallback')
)
),
$this->port()
);
$server->run();
echo "if the server ever determines it should close, this will be printed.";
// when loop completed, run this function
function stopCallback() {
$server->loop->stop();
}
Server.php
namespace MyApp;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
class Server implements MessageComponentInterface {
protected $callback;
public function __construct($callback) {
$this->callback = $callback;
}
// custom function called elsewhere in this class whenever you want to close the server.
protected function close() {
call_user_func($this->callback);
}
}