【发布时间】:2020-02-13 12:41:47
【问题描述】:
我正在使用 laravel-websockets 包与 vuejs 和 laravel-echo 来创建一个实时聊天应用程序。一切正常,但私人和在线通道在前端不起作用(后端正常),我在控制台中没有错误。
我正在使用 laravel 5.8 并使用 artisan serve 运行它。当我将私人频道更改为公共频道时,它工作正常。
// in bootstrap.js
window.Echo = new Echo({
broadcaster: 'pusher',
key: '*********',
cluster: 'ap2',
'wsHost': window.location.hostname,
'wsPort': 6001,
disableStats: true
});
// in private vue component
Echo.private('privatechat.'+this.user.id)
.listen('PrivateMessageSent',(e)=>{
console.log('private message');
});
})
// in PrivateMessageSent.php
namespace App\Events;
use App\Http\Models\Chat;
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 PrivateMessageSent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Chat $message)
{
$this->message = $message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('privatechat.'.$this->message->receiver_id);
}
}
//在channels.php中
Broadcast::channel('privatechat.{receiverid}', function($user,$receiverid) {
return auth()->check();
});
【问题讨论】:
-
PrivateMessageSent是一个事件类吗?请编辑问题并添加它 -
我添加它。记得我登录了 websocket 面板,但它在前面不起作用
-
我更新了答案
标签: php laravel vue.js laravel-echo