【发布时间】: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