php - 关闭 Ratchet IOServer

标签 php ratchet

是否有终止 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/

相关文章:

php - 使用 Ajax 或 Socket 的推拉技术

php - 如何在每次迭代中以从上到下的方式旋转数组元素

php - 使用 php 通过 WebSocket 在单页 Web 应用程序中创建 "page change"

PHP fatal error : Class 'MyApp\Chat' not found in/MyApp/chat-server. php

javascript - Ratchet 删除/添加数据忽略 ="push"

php - 什么在 SilverStripe 表单中生成 .holder-required 类

php - 性能 text/html 与 application/json

php - 如何使用 css/php 显示背景图像

PHP Ratchet 网络套接字$conn->send($msg)!如何在客户端获取此消息?

PHP Composer 无法为我的项目解析 composer.json 文件