PHP web套接字在一段时间后超时


PHP web socket gets times out after some time

所以我是新的web套接字,特别是在php中,所以我尝试了这个名为socketo.me的助手库。每件事都很好,套接字连接得到消息,填充消息到所有客户端,但唯一的问题是它会超时后的一段时间,我不知道。有一天我离开套接字连接,第二天早上我来尝试连接,它没有连接,我必须重新启动套接字(服务器保持运行)。下面是我用来运行socket的代码。

public static function actioninitialize(){
    $server = IoServer::factory(
            new Chat(),
            28
    );
    $server->run();
}

28是我使用的端口,Chat()是我用来接收和填充消息的消息接口

 class Chat implements MessageComponentInterface {
protected $clients;
public function __construct() {
    $this->clients = new 'SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn) {
    // Store the new connection to send messages to later
    $this->clients->attach($conn);
    echo "New connection! ({$conn->resourceId})'n";
}
public function onMessage(ConnectionInterface $from, $msg) {


            foreach ($this->clients as $client) {
                    $client->send('eg' );
            }
}
public function onClose(ConnectionInterface $conn) {
    // The connection is closed, remove it, as we can no longer send it messages
    $this->clients->detach($conn);
    echo "Connection {$conn->resourceId} has disconnected'n";
}
public function onError(ConnectionInterface $conn, 'Exception $e) {
    echo "An error has occurred: {$e->getMessage()}'n";
    $conn->close();
}
}

所以,我看了他们的文档,但是找不到这个超时的东西是从哪里来的。我也看过php文档,看到了一些东西,但没有任何帮助。任何帮助都将不胜感激。谢谢。

  1. WebSocket协议有一个名为乒乓的保持连接机制。你可能想尝试在你的应用程序中启用它。或者你可以在消息中实现同样的事情。目的是向外界显示您的TCP连接正在被使用。

  2. 连接总是因为很多原因而中断,从意外关闭的浏览器选项卡到您的操作系统不喜欢长时间连接到您的ISP的NAT服务器崩溃。通常一个应用程序应该能够检测到一个失败的连接,删除它并创建一个新的,而不会对用户体验产生任何影响(尽管如果我们谈论的是聊天,重新连接消息会很好)。