我已成功设置Laravel Echo以收听一个事件。但是,当我尝试将另一个事件添加到同一个频道时,第一个事件是其中的一部分,客户端似乎无法识别这个新事件。但是,看看Laravel Horizon,我可以看到正在注册和处理的作业。有关为何可能发生这种情况的任何想法?
非工作事件代码:
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 ParticipantCreated 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('channelReceiveLiveData');
}
}
工作活动代码:
<?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 liveDataTrigger 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 Channel|array
*/
public function broadcastOn()
{
return new Channel('channelReceiveLiveData');
}
}
发布事件的控制器:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Participant;
use App\Test;
use App\Events\ParticipantCreated;
class ParticipantController extends Controller
{
public function create() {
event(new ParticipantCreated('test'));
}
}
设置Echo的VueJS组件:
// This one works
Echo.channel('channelReceiveLiveData')
.listen('liveDataTrigger', (e) => {
console.log('Received Live Event');
})
// This one does not work
.listen('ParticipantCreated', (e) => {
console.log('Received Participant Created');
});
使用laravel echo初始化的Bootstrap.js
import Echo from 'laravel-echo'
window.io = require('socket.io-client');
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
});
我决定尝试从ParticipantController触发两个事件,我得到了这个输出:
laravel-echo-server控制台输出:
CHANNEL channelReceiveLiveData
Channel: channelReceiveLiveData
Event: App\Events\liveDataTrigger
CHANNEL channelReceiveLiveData
Channel: channelReceiveLiveData
Event: App\Events\ParticipantCreated
答案 0 :(得分:1)
当您在私人频道上收听活动时,您需要通过Echo
与channel()
交换private()
来告诉Echo.private('channelReceiveLiveData')
.listen('ParticipantCreated', (e) => {
console.log('Received Participant Created');
});
:
pusher
我想这是因为对于私有频道,laravel-echo-server需要对Laravel应用程序执行身份验证。因此,请务必遵循安装laravel-echo-server的所有步骤,包括在config/broadcasting.php
中设置host
配置。不要忘记配置port
和'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_KEY'),
'secret' => null,
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'host' => 'localhost',
'port' => 6001,
],
],
,如下所示:
public static boolean mySplit(int[] nums)
{
return MySplit(nums,0,0,0);
}
public static boolean MySplit(int[]arr,int result_5,int result_3,int pointer)//2 groups: 'result_5' & 'result_3' and pointer to iterate through an array
{
if(arr[pointer]%5==0)
{
return MySplit(arr,result_5+arr[pointer],result_3,pointer++);
}
else if(arr[pointer]%3==0)
{
return MySplit(arr,result_5,result_3+arr[pointer],pointer++);
}
}