我在Laravel 5.7服务器上的API上有一个轮询路由,其中api用户可以在上次轮询后请求任何信息.
如果有新信息返回$this-> prepareResult($newData),那么简单的部分是立即响应有效请求;
如果没有新数据,我将在数据库中存储轮询请求,然后cron实用程序可以每分钟检查一次所有轮询请求,并响应任何已更新数据的轮询.或者,我可以为数据更新创建一个事件监听器,并在更新数据时触发对轮询的响应.
我坚持如何恢复每个会话以匹配等待更新的设备.我可以存储或传递会话ID,但是如何确保CRON任务/事件处理器能够响应正确的IP地址,就像它对原始请求一样. PHP甚至可以这样做吗?
我试图避免websockets,因为会有很多设备,但有限的更新/交互.
长轮询是一种有效的技术.我认为用会话运行民意调查是一个坏主意.因为会话仅适用于原始用户.你可以使用PHP cli运行你的长轮询.您可以检查您的中间件以仅允许cli进行路由轮询.你可以使用
pthreads
通过cli运行你的长轮询使用pthreads.而现在pthreads v3在CLI以外的任何地方都安全且合理地设计.你可以使用你的cron每一小时触发你的线程.然后在你的控制器中你需要存储$time = time();标记您的执行开始时间.然后创建dowhile循环以循环您的轮询过程.条件可以是($time> time()3600)或其他条件.内部循环你需要检查是否存在民意调查?如果是,则运行它.然后在内部循环线的底部你需要睡一段时间,例如2秒.
通过cli运行你的长轮询使用pthreads.而现在pthreads v3在CLI以外的任何地方都安全且合理地设计.你可以使用你的cron每一小时触发你的线程.然后在你的控制器中你需要存储$time = time();标记您的执行开始时间.然后创建dowhile循环以循环您的轮询过程.条件可以是($time> time()3600)或其他条件.内部循环你需要检查是否存在民意调查?如果是,则运行它.然后在内部循环线的底部你需要睡一段时间,例如2秒.
在你的background.PHP上(这个文件由cron执行)
- <?PHP
- error_reporting(-1);
- ini_set('display_errors',1);
- class Atomic extends Threaded {
- public function __construct($data = NULL) {
- $this->data = $data;
- }
- private $data;
- private $method;
- private $class;
- private $config;
- }
- class Task extends Thread {
- public function __construct(Atomic $atomic) {
- $this->atomic = $atomic;
- }
- public function run() {
- $this->atomic->synchronized(function($atomic)
- {
- chdir($atomic->config['root']);
- $exec_statement = array(
- "PHP7.2.7",$atomic->config['index'],$atomic->class,$atomic->method
- );
- echo "Running Command".PHP_EOL. implode(" ",$exec_statement)." at: ".date("Y-m-d H:i:s").PHP_EOL;
- $data = shell_exec(implode(" ",$exec_statement));
- echo $data.PHP_EOL;
- },$this->atomic);
- }
- private $atomic;
- }
- $config = array(
- "root" => "/var/www/api.example.com/api/v1.1","index" => "index.PHP","interval_execution_time" => 200
- );
- chdir($config['root']);
- $threads = array();
- $list_threads = array(
- array(
- "class" => "Background_workers","method" => "send_email","total_thread" => 2
- ),array(
- "class" => "Background_workers","method" => "updating_data_user","method" => "sending_fcm_broadcast","total_thread" => 2
- )
- );
- for ($i=0; $i < count($list_threads); $i++)
- {
- $total_thread = $list_threads[$i]['total_thread'];
- for ($j=0; $j < $total_thread; $j++)
- {
- $atomic = new Atomic();
- $atomic->class = $list_threads[$i]['class'];
- $atomic->method = $list_threads[$i]['method'];
- $atomic->thread_number = $j;
- $atomic->config = $config;
- $threads[] = new Task($atomic);
- }
- }
- foreach ($threads as $thread) {
- $thread->start();
- usleep(200);
- }
- foreach ($threads as $thread)
- $thread->join();
- ?>
这在你的控制器上
- <?PHP
- defined('BASEPATH') OR exit('No direct script access allowed');
- class Background_workers extends MX_Controller {
- public function __construct()
- {
- parent::__construct();
- $this->load->database();
- $this->output->enable_profiler(FALSE);
- $this->configuration = $this->config->item("configuration_background_worker_module");
- }
- public function sending_fcm_broadcast() {
- $time_run = time();
- $time_stop = strtotime("+1 hour");
- do{
- $time_run = time();
- modules::run("Background_worker_module/sending_fcm_broadcast",$this->configuration["fcm_broadcast"]["limit"]);
- sleep(2);
- }
- while ($time_run < $time_stop);
- }
- }
这是来自codeigniter控制器的示例运行代码.