Php Laravel echo服务器在生产服务器中不工作
作为laravel echo的广播员,我对socket.io有问题 我试过什么:Php Laravel echo服务器在生产服务器中不工作,php,laravel,redis,socket.io,laravel-echo,Php,Laravel,Redis,Socket.io,Laravel Echo,作为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 f
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服务器控制台中看到它们。一切都在我的本地主机上运行,但在生产环境中没有,我失去了理智
以下是我的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'];
}
编辑
这是我的通知
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'];
}
正如我看到的,您在prodaction服务器中使用“https”协议,因此需要定义“sslCertPath”和“sslKeyPath”: 来自文档 使用SSL运行
- 客户端实现必须从访问socket.io客户端 https李>
- 服务器配置必须设置要使用的服务器主机 https
- 服务器配置应包括指向服务器上ssl证书和密钥的路径。
控制台.log(notification)
调用广播一个通知即可工作,因为您是在用户频道上专门调用通知功能的。有关更多详细信息,请参阅
广播及通知:
//Create a new notification with artisan, e.g. php artisan make:notification testNotification
class testNotification extends Notification {
...
public function via($notifiable)
{
return ['broadcast'];
}
...
public function toBroadcast($notifiable) {
return new BroadcastMessage([
'message' => 'TEST',
]);
}
}
然后,当您准备好广播您的消息时,只需通知用户:
$user->notify(new testNotification());
编辑:
此外,请确保您的用户模型中具有应呈报
特征:
class User extends Authenticatable
{
use Notifiable;
...
}
你能检查一下你是否在一个打开的ob_start()缓冲区下调用echo吗?@Miguel我在哪里可以这样做?也许这不是解决这个问题的最佳方法,但我会检查你正在使用的lavarel模块内部是否有使用这个缓冲区的地方。因为该功能正在获取标准输出,然后在以后检索它。当您在本地测试时,是否在
echo.run()
中使用与prod相同的authHost、主机和端口设置?我想知道这是否是防火墙或类似的访问问题…@Rich port和authost是相同的,主机因域名不同而不同。我不知道这是否是防火墙的问题,因为我可以把普通套接字服务器做得很好,从客户端连接和接收数据。一切都完成了。通过SSL。我刚刚写了“path/to”,因为我不想公开我的服务器pathk,正如我在日志中看到的一样,您收听的是“private user.1”频道,但日志中的redis广播到“PUBLISH”“private user.2”,问题可能在这里吗?刚刚复制了一个示例:两个用户在线。这就是用户2的原因。请确保您在通知类中定义了toBroadcast($notifiable)
函数,并返回广播消息,因为我在您的编辑中没有看到它。您需要以要测试的用户身份登录,并导航到有Echo通知调用的页面,然后设置一个测试路由以调用$user->notify(new testNotification())
在您登录的用户上,并在单独的窗口/选项卡中访问该页面。此外,请确保您的用户模型具有应呈报
特征。它具有应呈报特征!