【发布时间】:2023-03-21 18:43:01
【问题描述】:
我使用 Laravel 5.7 和 redis/laravel-echo-server 进行广播。 前端是vuejs 目前在公共频道广播也正在工作。 但在私人频道无法在前端接收套接字
Follow 是我的代码库。
此代码部分是用于认证的路由代码
Broadcast::channel('notification-{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
这是活动部分
class NotificationEvent implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* Create a new event instance.
*
* @return void
*/
public $user;
public function __construct(User $user)
{
//
$this->user = $user;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('notification-' . $this->user->id);
}
public function broadcastWith()
{
return ['data' => 'this is message'];
}
}
这是前端代码
//bootstrap.js
window.Echo = new Echo({
broadcaster: "socket.io",
host: window.location.hostname + ":6001"
});
//接收Vue文件
export default {
mounted() {
Echo.private("notification-" + window.Laravel.user).listen(
"NotificationEvent",
e => {
console.log("there");
}
);
}
};
joining to private channel is okay 但我无法收到来自 laravel 事件的任何消息
Route::get('test-broadcast', function () {
$user = \App\User::findOrFail(1);
broadcast(new NotificationEvent($user));
});
[2019-02-22 10:46:28] local.INFO: Broadcasting [App\Events\PrivateNotificationProcedure] on channels [private-notification-procedure-1] with payload:
{
"socket": null
}
我不知道出了什么问题。
非常感谢
【问题讨论】:
-
你有解决办法吗?
标签: php laravel vue.js socket.io