【发布时间】:2019-01-27 09:56:25
【问题描述】:
我在 socket.io 作为带有 laravel echo 的广播公司时遇到问题。
我尝试了什么:
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 控制台中完美地看到 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;
});
这是我的 echo 服务器配置,所有路径都正确。我在本地主机上测试了相同的文件,一切正常:
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'];
}
【问题讨论】:
-
你能检查你是否在打开的 ob_start() 缓冲区下调用 echo 吗?
-
@Miguel 我在哪里可以做到这一点?
-
可能这不是解决问题的最佳方法,但我会检查您正在使用的 lavarel 模块内部是否存在使用此缓冲区的某个点。因为该功能正在获取标准输出,然后再检索它
-
当您在本地测试时,您是否在
echo.run()中使用了相同的 authHost、host 和 port 设置作为 prod ?我想知道这是否是防火墙或类似的访问问题... -
@Rich 端口和 authost 相同,host 因域名不同。我不知道这是否是防火墙问题,因为我可以很好地做普通的套接字服务器并连接并从客户端接收数据。
标签: php laravel redis socket.io laravel-echo