[英]Laravel Echo does not listen for public channel events?
I am using Laravel Websockets package.Everything looks great on server side but Laravel Echo is not listening to events.. MySocketEvent.php: I am using Laravel Websockets package.Everything looks great on server side but Laravel Echo is not listening to events.. MySocketEvent.php:
<?php
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 MySocketEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $data;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($data)
{
//
$this->data=$data;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new Channel('DemoChannel.1');
}
public function broadcastAs()
{
return 'testing';
}
}
Web.php Web.php
Route::get('tests',function(){
$arr=['some'=>'some'];
broadcast(new \App\Events\MySocketEvent($arr));
return view('hi');
});
Bootstrap.js引导程序.js
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Echo = new Echo({
broadcaster: 'pusher',
//key: process.env.MIX_PUSHER_APP_KEY,
key: 'my-key',
wsHost: window.location.hostname,
wsPort: 6001,
wssPort: 6001,
disableStats: true,
cluster: 'ap2',
//cluster: process.env.MIX_PUSHER_APP_CLUSTER,
//encrypted: true
});
//subscribe-for-the-test-demo-channel-and-listen
window.Echo.channel('DemoChannel.1')
.listen('.testing', (e) => {
console.log("Received Data: ");
console.log(e);
});
I see the data sent from server by using php artisan WebSockets:serve but Laravel Echo does not listen.我看到使用 php artisan WebSockets:serve 从服务器发送的数据,但 Laravel Echo 不听。 Please help.请帮忙。 I'm 100% sure problem is with Laravel Echo.我 100% 确定问题出在 Laravel Echo 上。
The "dot" before "testing" “测试”之前的“点”
Broadcasting As " testing "广播作为“测试”
Listening For " .testing "监听“ .testing ”
Replace this替换这个
window.Echo.channel('DemoChannel.1')
.listen('.testing', (e) => {
console.log("Received Data: ");
console.log(e);
});
To this对此
window.Echo.channel('DemoChannel.1')
.listen('testing', (e) => {
console.log("Received Data: ");
console.log(e);
});
Update:更新:
Add this line to your event file将此行添加到您的事件文件
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
Replace this替换这个
class MySocketEvent implements ShouldBroadcast
To this对此
class MySocketEvent implements ShouldBroadcastNow
I was able to get Echo working by downgrading pusher to 4.4.0我能够通过将推送器降级到 4.4.0 来让 Echo 工作
npm install pusher-js@4.4.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.