试图让Pusher使用Laravel 5.1

时间:2015-08-07 05:27:09

标签: laravel laravel-5.1

我试图使用Pusher将Laravel 5.1广播到我的前端JS。

这是我的前端代码:

<!DOCTYPE html>
<head>
  <title>Pusher Test</title>
  <script src="https://js.pusher.com/2.2/pusher.min.js"></script>
  <script>
    // Enable pusher logging - don't include this in production
    Pusher.log = function(message) {
      if (window.console && window.console.log) {
        window.console.log(message);
      }
    };

    var pusher = new Pusher('2f89b1f8bcf257af0d8e', {
      encrypted: true
    });

    var channel = pusher.subscribe('test_channel');

    channel.bind('App\Events\NewChatMessage', function(message) {
      console.log(message);
    });
  </script>
</head>

这是我的活动:

<?php

namespace App\Events;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class NewChatMessage implements ShouldBroadcast
{
    use SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn()
    {
        return ['test_channel'];
    }
}

我的密钥是在broadcasting.php配置文件中设置的。

这些是我的路线:

/*
 * Pusher Test Routes
 */

Route::get('/fire', function() {
    event(new App\Events\NewChatMessage($message = ['name' => 'Martin']));

    return "Done";
});

Route::get('/test', function() {
    return view('test');
});

出于某种原因,我没有收到回复!

前端代码很好,因为我可以使用pusher调试控制台接收数据,所以问题似乎出现在Laravel网站上。

任何帮助将不胜感激:D

1 个答案:

答案 0 :(得分:0)

Laracasts有一个很好的视频,涵盖了这个主题。看着这应该让你前进:https://laracasts.com/lessons/broadcasting-events-in-laravel-5-1