是否有终止 IOServer
的 loop
的方法?
我正在使用 WebSockets 作为一个 hacky 应用程序间通信系统(相信我,如果我可以使用其他任何东西,我会的),但我无法跳出循环并在调用 后继续我的应用程序在
。IOServer
上运行 ()
我是否需要将 IOServer
子类化为 TerminatableIOServer
或此功能是否已经存在?
最佳答案
在 IOServer
的循环中调用 stop()
。
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);
}
}
关于php - 关闭 Ratchet IOServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21650935/