【问题标题】:Trying to get Pusher working with Laravel 5.1试图让 Pusher 与 Laravel 5.1 一起工作
【发布时间】:2015-08-07 05:27:08
【问题描述】:

我正在尝试使用 Laravel 5.1 使用 Pusher 向我的前端 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');
});

由于某种原因,我没有得到回复!

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

任何帮助将不胜感激:D

【问题讨论】:

  • 您没有创建公共类变量作为事件的有效负载。

标签: laravel laravel-5.1


【解决方案1】:

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

【讨论】:

猜你喜欢
  • 2019-02-04
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-01
  • 2017-12-22
相关资源
最近更新 更多