2014-02-08 42 views
1

有没有方法终止IOServerloop关闭棘轮IOServer

我使用WebSockets作为hacky的应用程序间通信系统(相信我,如果我可以使用其他任何东西,我会),但是我无法打开循环并在调用run()后继续我的应用程序IOServer

我需要将IOServer分为TerminatableIOServer还是此功能已存在?

回答

3

致电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); 
    } 
} 
+0

是有办法做到这一点如果循环将被提前终止。例如,如果脚本因某种随机原因而崩溃或关闭,那么如何关闭此端口以便它可以再次使用? –

+0

@DanHastings杀死端口上的锁定的PHP进程应该工作 – citelao

+0

真棒,你让我的一天:) – sumit