关闭棘轮 IOServer

cit*_*lao 3 php ratchet

是否有终止的一种方法IOServerloop

我使用的WebSockets作为哈克-应用间通信系统(相信我,如果我可以使用别的,我会),但我不能破环了,继续打完电话后我的应用程序run()IOServer

我需要子IOServerTerminatableIOServer或此功能已经存在?

cit*_*lao 6

呼叫stop()IOServer的循环。

启动器.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();
}
Run Code Online (Sandbox Code Playgroud)

服务器.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);
    }
}
Run Code Online (Sandbox Code Playgroud)