gpt4 book ai didi

php - 关闭 Ratchet IOServer

转载 作者:行者123 更新时间:2023-12-04 02:08:31 33 4
gpt4 key购买 nike

是否有终止 IOServerloop 的方法?

我正在使用 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com