droi5225 2016-10-31 07:55
浏览 181

PHP websocket在一段时间后会超时

So I'm new in web sockets specially in php so i tried this helper library called socketo.me. Every thing works fine socket connects gets messages, populates messages to all clients but only problem is it gets timed out after sometime that i dont know. One day i leave socket connected and the very next morning I come and try to connect and its not connecting and i have to restart socket (Server keeps running). Here is the code im using to run socket.

public static function actioninitialize(){

    $server = IoServer::factory(
            new Chat(),
            28
    );

    $server->run();

}

28 is the port im using and Chat() is the message interface im using to receive and populate messages

 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})
";
}

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
";
}

public function onError(ConnectionInterface $conn, \Exception $e) {
    echo "An error has occurred: {$e->getMessage()}
";

    $conn->close();
}
}

So, iv seen their documentation and all but couldnt find where is this timeout thing coming from. I have seen php documentation aswell and saw some things and tried but nothing helped. Any help will be appreciated. Thanks.

  • 写回答

1条回答 默认 最新

  • dongxinm279890 2016-10-31 08:34
    关注
    1. WebSocket protocol has a keepalive mechanism named ping-pong. You may want to try to enable it in your app. Alternatively you can implement the same thing with messages. The goal is to show the world that your TCP connection is being used.

    2. Connections break all the time for a lot of reasons from accidentally closed browser tab to your OS not liking long connections to a NAT server crash at your ISP. Usually an app should be able to detect a failed connection, drop it and create the new one without any impact on user experience (though reconnecting message would be nice if we're talking about a chat).

    评论

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型