0
我想不通,爲什麼我無法實現這Laravel 5.3 爲了測試我只是做純PHP和JavaScript:Laravel服務器推送
的PHP:
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$val = 20;
echo "data: The value is: " . $val . "\n\n";
flush();
而且JS:
var source;
$(document).ready(function() {
source = new EventSource("/eventeg.php");
source.onmessage = function (evnt) {
document.getElementById("serverevent").innerHTML = evnt.data;
};
});
當我更改PHP文件時,頁面反映'serverevent'內的推送。
不過只是試圖將轉換爲Laravel:
PHP:
在路線web.php
Route::any('event', '[email protected]Test');
然後 - (創建可重用的代碼,我有一個回購作爲由命名空間顯示)
<?php
namespace App\DD_laravelAp\Controllers;
class RootEvent_c extends RootController_c {
protected function start($init = null) {
}
public function eventTest() {
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$val = 10;
echo "data: The value is: " . $val . "\n\n";
flush();
}
}
最後的類Laravel應該使用:
<?php
namespace App\Http\Controllers;
use App\DD_laravelAp\Controllers\RootEvent_c;
class Event_c extends RootEvent_c {
}
隨着JS改變以反映Laravel URL結構:
source = new EventSource("/event");
source.onmessage = function (evnt) {
document.getElementById("serverevent").innerHTML = evnt.data;
};
從控制檯的錯誤卻是:
Firefox can't establish a connection to the server at http://domain/event