我有推送应用程序,然后我尝试通过laravel事件向pusher发送消息,这是我的事件:
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewOrder
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $email;
public $message;
public function __construct($email)
{
$this->email = $email;
$this->message = "New order from {$email}, please check.";
//
}
public function broadcastOn()
{
return ['new-order'];
}
}
我试着用路线测试来测试它,
Route::get('test', function () {
event(new App\Events\NewOrder('John'));
return "Event has been sent!";
});
我的推送器配置已配置为我的推送器帐户配置,但在访问/test
后,推送器调试控制台无法显示任何内容。
'default' => env('BROADCAST_DRIVER', 'pusher'),
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
// 'cluster' => 'ap1',
'encrypted' => true,
],
],
'redis' => [
'driver' => 'redis',
'connection' => 'default',
],
'log' => [
'driver' => 'log',
],
'null' => [
'driver' => 'null',
],
],
];
我不知道从laravel应用程序看到推送者的日志,任何人都可以帮助我吗? 我正在使用laravel 5.5
答案 0 :(得分:2)
我认为问题出在您的事件类声明上,您应该实现ShouldBroadcastNow
,因为它会立即触发事件并且不需要队列实现。默认情况下,您需要设置一个队列,然后运行该队列以引发事件。
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class NewOrder implements ShouldBroadcastNow
{
....do your stuff....
}
还要检查this amazing playlist,这将解决su的所有问题
答案 1 :(得分:2)
我也有大约2个小时的问题。
您忘记实施广播了,
您可以使用:
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
class NewOrder implements ShouldBroadcastNow
或
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewOrder implements ShouldBroadcast
后者则需要队列驱动程序
快乐编码:)
答案 2 :(得分:1)
嗨,我也遇到了这个问题,并通过清除缓存来解决它!这是代码!
php artisan config:cache
php artisan config:clear
php artisan route:cache
php artisan route:clear
php artisan optimize --force
希望对您有帮助!
答案 3 :(得分:0)
替换此:
class NewOrder
与此:
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
class NewOrder implements ShouldBroadcast
应该可以。让我知道我是否可以提供更多帮助。
答案 4 :(得分:0)
在这里我如何解决该错误
在页面中添加csrf_token元标记
Pusher.logToConsole = true; var key =''; var pusher = new Pusher(key,{ 群集:“ ap2”, forceTLS:true }); var channel = pusher.subscribe('channel-name'); channel.bind('App \ Events \ EventName',function(data){ console.log(“ OK”); console.log(data); });
答案 5 :(得分:0)
'useTLS' => true
选项解决了我的问题。
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'useTLS' => true,
]
答案 6 :(得分:0)
检查 .env 文件 QUEUE_CONNECTION=sync
或您必须运行 php artisan queue:work