Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

angular - PHP long-polling request issue

I am new to the Long-polling requests concept and having an issue with the requests from an Angular project to PHP XAMPP local server. When the request time limit is set to 0 like

set_time_limit(0);

The requests stay alive forever. Even after the client reloads the website page or closes the whole tab the request won't stop processing the script and keep repeating it. If the client reloads the website page 10 times then closes the tab, the PHP server will keep processing 10 different requests for no one.

I am wondering how to avoid that or, how to exit the request once the client is not going to receive its response by reloading/leaving the page?

my api code:

            set_time_limit(0);
            $accountId = 1;
            $database = DBManager::link();

            while (true) {

                $database->call('account_get_notifications_updates')
                    ->value('_accountId', $accountId, DBParams::INT);

                if ($database->run()) {

                    $sqlResults  = $database->fetchArray();
                    $returnValue = $database->getReturnValue();
                    
                    if ($returnValue == 0) {
                        // add the new notifications to the response and exit
                        $this->Response->SetData($sqlResults['dataSet2']);
                        break;
                    } else {
                        sleep(30);
                        continue;
                    }
                }

                throw new Exception($database->getError()->message);
            }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...