doubi12138 2014-02-08 19:43
浏览 21
已采纳

关闭棘轮IOServer

Is there a method of terminating IOServer's loop?

I'm using WebSockets as a hacky inter-app communication system (believe me, if I could use anything else, I would), but I can't break out of the loop and continue my application after calling run() on IOServer.

Do I need to subclass IOServer into TerminatableIOServer or does this feature already exist?

  • 写回答

1条回答 默认 最新

  • dongyong8098 2014-02-08 20:04
    关注

    Call stop() on the IOServer's loop.

    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);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序