Laravel服务器推送

时间:2017-02-05 16:34:20

标签: javascript laravel events server

我无法理解为什么我无法在Laravel 5.3中实现这一点 为了测试我只是做了纯PHP和javascript:

PHP:

    var source;
    $(document).ready(function () {
      source = new EventSource("/eventeg.php");
      source.onmessage = function (evnt) {
        document.getElementById("serverevent").innerHTML = evnt.data;
      };
    });

和js:

Route::any('event', 'Event_c@eventTest');

一旦我更改了PHP文件,页面就会反映出'serverevent'中的推送。

然而,只是尝试转换为Laravel:

PHP:

在路线web.php

    <?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();
  }

}

然后 - (为了创建可重用的代码,我有一个如命名空间所示的repo)

<?php

namespace App\Http\Controllers;

use App\DD_laravelAp\Controllers\RootEvent_c;

class Event_c extends RootEvent_c {

}

最后是Laravel应该使用的课程:

  source = new EventSource("/event");
  source.onmessage = function (evnt) {
    document.getElementById("serverevent").innerHTML = evnt.data;
  };

更改js以反映Laravel网址结构:

Firefox can't establish a connection to the server at http://domain/event

然而,控制台的错误是:

{{1}}

1 个答案:

答案 0 :(得分:0)

升级到更新版本的Laravel以解决此问题。