我使用laravel echo作为广播器时,遇到了socket.io的问题。
我尝试的方法:
php artisan cache:clear
php artisan config:clear
我可以在日志中看到用户的连接:
0|Socket-Connection | [11:17:00 AM] - ********** joined channel: test-channel
0|Socket-Connection | [11:17:01 AM] - ********** authenticated for: private-user.1
0|Socket-Connection | [11:17:01 AM] - ********** joined channel: private-user.1
我的队列正在运行,并且可以正确记录所有事件。
我可以在Redis控制台中完美地看到我的事件和数据库通知。
但是没有事件被广播,我在laravel-echo-server控制台中也没有看到它们。 在我的本地环境中一切正常,但在生产环境中却无法工作,让我感到非常苦恼。
这是我的 Laravel Echo JS:
if (typeof io !== 'undefined') {
console.log(window.location.origin);
window.Echo = new Echo({
broadcaster: 'socket.io',
host: window.location.origin + ':6001',
auth: {
headers: {
Authorization: 'Bearer ' + bearerToken,
},
}
});
window.Echo.private('user.' + user_id).notification((notification) => {
console.log(notification);
});
}
在我的用户模型中,我定义了这个:
/**
* @return string
*/
public function receivesBroadcastNotificationsOn()
{
return 'user.' . $this->id;
}
而在我的频道中,我有这个:
Broadcast::channel('user.{id}', function ($user, $id) {
return (int)$user->id === (int)$id;
});
这是我的回显服务器配置,所有路径都正确。我已在本地主机上测试了相同的文件并且一切正常:
这是我的回显服务器配置,所有路径都是正确的。我已经在我的本地主机上测试了同样的文件,一切都正常:
var echo = require('laravel-echo-server/dist');
echo.run({
"appKey": "myappkey",
"authHost": "https://url",
"authEndpoint": "/broadcasting/auth",
"database": "redis",
"clients": [
{
"appId": "myappid",
"key": "mykey"
}
],
"databaseConfig": {
"redis": {
"port": "6379",
"host": "myhost",
"password": "mysupersecretpassword"
},
"sqlite": {
"databasePath": "/database/laravel-echo-server.sqlite"
}
},
"devMode": true,
"host": "url",
"port": "6001",
"protocol": "https",
"referrers": [],
"sslCertPath": "/path/to/certificate.pem",
"sslKeyPath": "/path/to/key",
"verifyAuthPath": true,
"verifyAuthServer": false
});
当我发布数据库通知时,我的Redis日志显示了这个:
1534840681.110359 [0 "IP ADDRESS HERE"] "PUBLISH" "private-user.2" "{\"event\":\"Illuminate\\\\Notifications\\\\Events\\\\BroadcastNotificationCreated\",\"data\":{\"title\":\"Ravim CONVULEX 50MG\\/ML staatust muudeti\",\"notification_type\":\"element-soft-delete\",\"message\":\"Ravimi CONVULEX 50MG\\/ML staatust muutis kasutaja Kalle \",\"url\":\"https:\\/\\/www.app.riskomed.com\\/admin\\/brands\\/all\",\"id\":\"30c37d0d-c39b-41bf-93fc-afa0c78ca9db\",\"type\":\"App\\\\Notifications\\\\API\\\\Management\\\\Medical\\\\Brands\\\\BrandSoftDeleteNotification\",\"socket\":null},\"socket\":null}"
编辑
这是我的通知
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return ['mail', 'database', 'broadcast'];
}
echo.run()
中使用了与生产环境相同的authHost、host和port设置?我想知道这是否是防火墙或类似访问问题... - Richartisan queue:restart
命令,检查是否可以解决你的问题... - Bart