关闭棘轮IOServer

时间:2014-02-08 19:43:59

标签: php ratchet

是否有方法终止IOServer的{​​{1}}?

我正在使用WebSockets作为一个hacky应用程序间通信系统(相信我,如果我可以使用其他任何东西,我会),但我不能打破循环并在调用{{1后继续我的应用程序在loop上。

我是否需要将run()细分为IOServer或该功能是否已存在?

2 个答案:

答案 0 :(得分:4)

stop()的循环中调用IOServer

<强> 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);
    }
}

答案 1 :(得分:0)

function stopCallback() {
  global $server;
  $server->loop->stop();
}

如果你不这样做,就会出错。