3

有没有终止的IOServer方法loop

我正在使用 WebSockets 作为一个骇人听闻的应用程序间通信系统(相信我,如果我可以使用其他任何东西,我会),但我无法跳出循环并在run()调用IOServer.

我需要子类化IOServer还是TerminatableIOServer这个功能已经存在?

4

2 回答 2

7

调用' 循环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);
    }
}
于 2014-02-08T20:04:40.947 回答
0
function stopCallback() {
  global $server;
  $server->loop->stop();
}

如果你不这样做,它会给出错误。

于 2021-06-03T18:03:54.090 回答