在Laravel 5.8中,当我尝试listen
到专用频道(使用Pusher)时,我在控制台中收到此错误,而且令人惊讶的是,我在Web上找不到任何谈论此错误的地方(特别是首先部分,某处谈论了状态500,但没有帮助。):
无法从auth端点检索auth字符串-从/ broadcasting / auth接收到状态500。必须对客户端进行身份验证才能加入私人或在线渠道。
Notification.php
class Notification implements ShouldBroadcastNow
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
/**
*
*
* @param User $user
*/
public function __construct(User $user)
{
$this->user = $user;
}
public function broadcastOn()
{
return new PrivateChannel('notif.'.$this->user->id);
}
public function broadcastAs()
{
return 'Notification';
}
}
Channels.php
Broadcast::channel('notif.{id}', function ($user, $id) {
//return (int) $user->id === (int) $id;
return true;
});
app.js
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
Pusher.logToConsole = true;
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'WORKING KEY',
cluster : "eu",
encrypted: false,
csrfToken: 'WORKING TOKEN',
});
window.Echo.private('notif.${id}').listen('.Notification', function (e) {
console.log(e);
});
我不需要API授权,项目是基于Web的。
答案 0 :(得分:0)
根据此处的本教程 https://laracasts.com/series/get-real-with-laravel-echo/episodes/1
确保您已在应用程序服务提供者下的config / app.php文件中取消对BroadcastServiceProvider.php的注释。
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
,以便路由可以浏览Channels.php。
如果仍然无法正常工作,则可以尝试在BroadcastServiceProvider.php中添加一些身份验证,即通过添加auth:api这样的检查:
public function boot()
{
Broadcast::routes(['middleware' => ['auth:api']]);
require base_path('routes/channels.php');
}
如果您在api.php路由内使用auth链接。
#EDIT 1
对于任何身份验证问题,您都可以尝试在app / Http / Kernel.php中添加此行
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
..other middleware groups,
\Laravel\Passport\Http\Middleware\CreateFreshApiToken::class,
],
要消耗auth令牌,如Laravel文档中所述: https://laravel.com/docs/5.6/passport#consuming-your-api-with-javascript
答案 1 :(得分:0)
我认为您那里缺少一个参数,请尝试以下操作:
import Echo from 'laravel-echo'
window.Pusher = require('pusher-js');
Pusher.logToConsole = true;
window.Echo = new Echo({
broadcaster: 'pusher',
key: 'WORKING KEY',
cluster : "eu",
encrypted: true,<-----
csrfToken: 'WORKING TOKEN',
authEndpoint: 'https://yourwebsitename.com/broadcasting/auth', <-----
});
window.Echo.private('notif.${id}').listen('.Notification', function (e) {
console.log(e);
});