有没有终止的IOServer
方法loop
?
我正在使用 WebSockets 作为一个骇人听闻的应用程序间通信系统(相信我,如果我可以使用其他任何东西,我会),但我无法跳出循环并在run()
调用IOServer
.
我需要子类化IOServer
还是TerminatableIOServer
这个功能已经存在?
调用' 循环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();
}
服务器.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);
}
}
function stopCallback() {
global $server;
$server->loop->stop();
}
如果你不这样做,它会给出错误。