Laravel 推送器未在本地主机中进行身份验证,但在开发服务器中进行了身份验证
Laravel pusher not authenticated in Localhost but authenticated in Development Server
我刚刚在私人频道中设置了带有推送器的 Laravel 通知。在开发服务器 (127.0.0.1:8000) 上一切正常。我定期收到通知,但是当我尝试从本地主机 (http://localhost/) 运行 时收到身份验证错误“http://localhost/broadcasting/auth 404 未找到”。这是我的代码:
Reportnotification.php
public function toArray($notifiable)
{
return [
'message'=>$this->details,
'link'=> $this->link,
];
}
public function toBroadcast($notifiable): BroadcastMessage
{
return new BroadcastMessage([
'message' => "$this->details",
'link'=> "$this->link"
]);
}
Events/ReportNotification
class ReportNotification implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
// public $username;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$message = $this->message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('notification');
}
Listeners/SendReportNotification
public function handle($event)
{
$admins = User::where('role',1)->get();
ReportNotification::send($admins, new ReportNotification($event->user));
}
Channels.php
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
master.blade.php
<script>
var id = {{Auth::user()->id}}
Echo.private('App.Models.User.'+id)
.notification((ReportNotification) => {
console.log(ReportNotification.message);
console.log(ReportNotification.link);
});
</script>
这些是我的代码。在 "127.0.0.1:8000" 中一切正常,但在 "localhost"
中无法正常工作
localhost
名称通常解析为 127.0.0.1
。
当您打开 http://localhost
时,它会解析 127.0.0.1:80
,因为 80
是 HTTP 协议的默认端口号。
您的服务器正在按照您所说的收听 8000
。所以你应该尝试 localhost:8000
或者将你的网络服务器使用的端口更改为 80
.
我刚刚在私人频道中设置了带有推送器的 Laravel 通知。在开发服务器 (127.0.0.1:8000) 上一切正常。我定期收到通知,但是当我尝试从本地主机 (http://localhost/) 运行 时收到身份验证错误“http://localhost/broadcasting/auth 404 未找到”。这是我的代码:
Reportnotification.php
public function toArray($notifiable)
{
return [
'message'=>$this->details,
'link'=> $this->link,
];
}
public function toBroadcast($notifiable): BroadcastMessage
{
return new BroadcastMessage([
'message' => "$this->details",
'link'=> "$this->link"
]);
}
Events/ReportNotification
class ReportNotification implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
// public $username;
public $message;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message)
{
$message = $this->message;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('notification');
}
Listeners/SendReportNotification
public function handle($event)
{
$admins = User::where('role',1)->get();
ReportNotification::send($admins, new ReportNotification($event->user));
}
Channels.php
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
master.blade.php
<script>
var id = {{Auth::user()->id}}
Echo.private('App.Models.User.'+id)
.notification((ReportNotification) => {
console.log(ReportNotification.message);
console.log(ReportNotification.link);
});
</script>
这些是我的代码。在 "127.0.0.1:8000" 中一切正常,但在 "localhost"
中无法正常工作localhost
名称通常解析为 127.0.0.1
。
当您打开 http://localhost
时,它会解析 127.0.0.1:80
,因为 80
是 HTTP 协议的默认端口号。
您的服务器正在按照您所说的收听 8000
。所以你应该尝试 localhost:8000
或者将你的网络服务器使用的端口更改为 80
.