Laravel 4 服务器在Laravel内部发送了事件
我试图在Laravel4中实现HTML5服务器发送的事件,我发现了这个 脚本使用Laravel 4 服务器在Laravel内部发送了事件,laravel-4,server-sent-events,Laravel 4,Server Sent Events,我试图在Laravel4中实现HTML5服务器发送的事件,我发现了这个 脚本使用server.php文件使其工作。我的问题是,我将如何实现它,使它与控制器而不是普通的php文件一起工作?因此,在Javascript端,它将如下所示: var source=EventSource('/notifications') 我试着这么做只是想看看是否得到回应: class NotificationsController extends BaseController { public function i
server.php
文件使其工作。我的问题是,我将如何实现它,使它与控制器而不是普通的php文件一起工作?因此,在Javascript端,它将如下所示:
var source=EventSource('/notifications')代码>
我试着这么做只是想看看是否得到回应:
class NotificationsController extends BaseController {
public function index()
{
$response = Response::view('notifications.index');
$response->header('Content-Type', 'text/event-stream');
$response->header('Cache-Control', 'no-cache');
return $response;
}
}
并且认为:
<?php
echo "id: 4" . PHP_EOL;
echo "data: this is a message" . PHP_EOL;
flush();
找到它后,解决方案非常简单:
主布局中的JavaScript
:
var source = new EventSource('/notifications');
source.onmessage = function(e) {
console.log(e);
}
在NotificationsController@index
:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
if (true)
{
echo "data: <p> Hello There </p>\n";
}
flush();
标题(“内容类型:文本/事件流”);
标头(“缓存控制:无缓存”);
如果(真)
{
echo“数据:你好\n”;
}
冲洗();
我收到消息了 找到它,解决方案非常简单:
主布局中的JavaScript
:
var source = new EventSource('/notifications');
source.onmessage = function(e) {
console.log(e);
}
在NotificationsController@index
:
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
if (true)
{
echo "data: <p> Hello There </p>\n";
}
flush();
标题(“内容类型:文本/事件流”);
标头(“缓存控制:无缓存”);
如果(真)
{
echo“数据:你好\n”;
}
冲洗();
我收到消息了