Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k 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 OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...