我正在使用laravelecho服务器、redis和socket.io来发送聊天信息。
我正在使用广播事件。
event(new MessageSent($messageThread));
这是方法的专用通道 brodcastOn
具体如下:
public function broadcastOn()
{
return new PrivateChannel('message.' . $this->broadcastUser->id);
}
我的 routes/channels.php
看起来像:
Broadcast::channel('message.*', function ($user, $id) {
return (int) $user->id === (int) $id;
});
下面是我的 laravel-echo-server.json
:
{
"authHost": "http://localhost:8000",
"authEndpoint": "/broadcasting/auth",
"clients": [
{
"appId": "******",
"key": "*************"
}
],
"database": "redis",
"databaseConfig": {
"redis": {
"port":"6379",
"host":"127.0.0.1",
"db":"0",
"password":"****"
}
},
"devMode": true,
"host": null,
"port": "6001",
"protocol": "http",
"socketio": {},
"secureOptions": 67108864,
"sslCertPath": "",
"sslKeyPath": "",
"sslCertChainPath": "",
"sslPassphrase": "",
"subscribers": {
"http": true,
"redis": true
}
用于侦听事件的jquery:
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.hostname + ':6001',
client: socketio,
auth: {headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr("content"),
}},
});
var userId = window.APP.user;
const channel = window.Echo.channel(`app_database_private-message.${userId}`);
channel.listen('MessageSent', (payload) => {
console.log('payload', payload)
});
这是我的redis部分 config/database.php
:
'redis' => [
'client' => env('REDIS_CLIENT', 'predis'),
'options' => [
'cluster' => env('REDIS_CLUSTER', 'predis'),
'prefix' => Str::slug(env('APP_NAME', 'laravel'), '_').'_database_',
],
'default' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_DB', 0),
],
'cache' => [
'host' => env('REDIS_HOST', '127.0.0.1'),
'password' => env('REDIS_PASSWORD', null),
'port' => env('REDIS_PORT', 6379),
'database' => env('REDIS_CACHE_DB', 1),
],
],
显示以下错误:
L A R A V E L E C H O S E R V E R
version 1.6.0
⚠ Starting server in DEV mode...
✔ Running at localhost on port 6001
✔ Channels are ready.
✔ Listening for http events...
✔ Listening for redis events...
Server ready!
[1:21:22 PM] - Preparing authentication request to: http://localhost:8000
[1:21:22 PM] - Sending auth request to: http://localhost:8000/broadcasting/auth
⚠ [1:21:22 PM] - IQ7eT9P_RzF62lRSAAAA could not be authenticated to app_database_private-message.3
{ "message": "", "exception": "Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException", "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/Broadcaster.php", "line": 81, "trace": [ { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/Broadcasters/RedisBroadcaster.php", "line": 58, "function": "verifyUserCanAccessChannel", "class": "Illuminate\\Broadcasting\\Broadcasters\\Broadcaster", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php", "line": 319, "function": "auth", "class": "Illuminate\\Broadcasting\\Broadcasters\\RedisBroadcaster", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php", "line": 237, "function": "__call", "class": "Illuminate\\Broadcasting\\BroadcastManager", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastController.php", "line": 23, "function": "__callStatic", "class": "Illuminate\\Support\\Facades\\Facade", "type": "::" }, { "function": "authenticate", "class": "Illuminate\\Broadcasting\\BroadcastController", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Controller.php", "line": 54, "function": "call_user_func_array" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php", "line": 45, "function": "callAction", "class": "Illuminate\\Routing\\Controller", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 219, "function": "dispatch", "class": "Illuminate\\Routing\\ControllerDispatcher", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Route.php", "line": 176, "function": "runController", "class": "Illuminate\\Routing\\Route", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Router.php", "line": 680, "function": "run", "class": "Illuminate\\Routing\\Route", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 30, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Router", "type": "->" }, { "file": "/opt/edugem/apps/project/app/Http/Middleware/AppMiddleware.php", "line": 55, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "App\\Http\\Middleware\\AppMiddleware", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php", "line": 41, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Routing\\Middleware\\SubstituteBindings", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php", "line": 75, "function": "Illuminate\\Routing\\{closure}", "class": "Illuminate\\Routing\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php", "line": 163, "function": "handle", "class": "Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php", "line": 53, "function": "Illuminate\\Pipeline\\{closure}", "class": "Illuminate\\Pipeline\\Pipeline", "type": "->" }, { "file": "/opt/edugem/apps/project/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php", "line": 49, "function": "Illuminate\\Routing\\{closure}"
2条答案
按热度按时间4urapxun1#
您有两个问题:
在回声方面,你应该使用
您还必须确保laravel config变量
database.redis.options.prefix
设置为''
.在laravel方面,您需要实现一个通道的授权,以便允许套接字侦听这个私有通道。
https://laravel.com/docs/7.x/broadcasting#defining-授权回调
zbdgwd5y2#
而不是
使用此语句
在echo中的命名空间前使用点(.)
例子
然后重写
broadcastAs
在通知类中使用shouldBroadCastNow
: