简体   繁体   English

使用服务器发送的事件自动刷新浏览器

[英]Auto refresh browser using server sent events

I am using a third party API to call a URL on my server when I receive a phone call. 收到电话时,我正在使用第三方API来调用服务器上的URL。

I am trying to implement Server Sent Events in order to reload the browser when my URL is called by the API, however the page does not refresh when I do a POST test to the api_new_call function. 我正在尝试实现服务器发送事件,以便在API调用我的URL时重新加载浏览器,但是当我对api_new_call函数进行POST测试时,页面不会刷新。

I am using PHP (codeigniter) 我正在使用PHP(codeigniter)

Javascript: Javascript:

var source = new EventSource("/controller/new_call_trigger");
source.onmessage = function(event) {
    location.reload();
};

Controller.php: Controller.php:

public function api_new_call() {
    $called_number = $this->input->post('called_number');
    $calling_number = $this->input->post('calling_number');
    $new_cad = $this->Model->api_new_cad($calling_number);
    $this->new_call_trigger();
}


public function new_call_trigger() {
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    echo "data: TEST";
    flush();
}

You have this code 你有这个代码

header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
echo "data: TEST";
flush();

None of those headers refresh the page. 这些标题都没有刷新页面。 You'll want to add this. 您将要添加它。

header('Location: ' . $_SERVER['REQUEST_URI']);

Add it above the echo , or else you'll have a headers already sent error. 将其添加到echo之上,否则您将收到已发送标题的错误消息。

Also, do a check for post, otherwise you'll get a redirect too many times error 另外,请检查发布内容,否则您将收到重定向次数过多的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM