我在使用Laravel的Pusher时遇到了一些问题。我已经将我的channels.php文件配置为在此频道上广播(仅用于经过身份验证的用户)-
Broadcast::channel('user.{user}', function ($user, $id) {
return (int) $user->id === (int) $id;
});
我的.env文件说-
BROADCAST_DRIVER=pusher
PUSHER_APP_ID="652357"
PUSHER_APP_KEY="13e1**********55"
PUSHER_APP_SECRET="1e******667f8*****9f"
PUSHER_APP_CLUSTER=eu
我的广播网站php中有这个
'default' => env('BROADCAST_DRIVER', 'log'),
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER', 'eu'),
'encrypted' => true,
],
],
该事件在我的UsersController.php中触发
<?php
namespace App\Http\Controllers;
use App\Events\UserStatusEvent;
use App\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(['auth', 'profile'])->except('show', 'help');
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param string $user
* @return \Illuminate\Http\Response
*/
public function update(Request $request, User $user)
{
$data = $request->only(['first_name', 'last_name', 'middle_name', 'email', 'city', 'dob', 'gender', 'address']);
$user->update($data);
event(new UserStatusEvent(auth()->user()));
// if ($request->ajax()) {
// return response()->json([
// 'status' => 'Profile Update successful.',
// 'errors' => app('Illuminate\Http\Response')->status(),
// ], app('Illuminate\Http\Response')->status());
// }
return back();
}
}
我安装npm并下载了laravel-echo(在Laravel 5.7 Broadcasting Docs之后),并且在运行$ npm run dev
之前在bootstrap.js文件中,我具有以下内容-
import Echo from 'laravel-echo';
window.Pusher = require('pusher-js');
window.Pusher.logToConsole = true;
window.Echo = new Echo({
authEndpoint: 'http://localhost/penfs/public/broadcasting/auth',
broadcaster: 'pusher',
key: process.env.MIX_PUSHER_APP_KEY,
cluster: process.env.MIX_PUSHER_APP_CLUSTER,
encrypted: true
});
window.Echo.private(`user.${user}`)
.listen('UserStatusEvent', (e) => {
console.log(e.user.first_name);
console.log(e.user.email);
});
我遇到的第一个错误是控制台上的Pusher : No callbacks on private-user.[object HTMLDivElement] for pusher:subscription_error
,这里我假设未传递经过身份验证的用户的实例,因此我通过在其app.blade.php文件中插入以下内容来对其进行了调整-{ {1}}在我的{。{1}}编译的app.js脚本之前。
现在,我可以获取auth用户的实例,因为我在视图中对其进行了硬编码,但是随后遇到了另一个错误-<script type="text/javascript">var user = '{{ $user->id }}'</script>
。我一直在尝试调试整个今天。请我真的需要有人帮忙。谢谢。
哦,我在config / app.php中未进入npm run dev
修改:
-
Pusher : No callbacks on private-user.6 for pusher:subscription_error