首页 > 解决方案 > Laravel 回显服务器在生产服务器中不起作用

问题描述

作为带有 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 控制台中完美地看到 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'];
    }

标签: phplaravelredissocket.iolaravel-echo

解决方案


正如我看到你在生产服务器中使用'https'协议,所以你需要定义'sslCertPath'和'sslKeyPath':

来自文档

使用 SSL 运行

  • 您的客户端实现必须从 https 访问 socket.io 客户端。
  • 服务器配置必须将服务器主机设置为使用 https。
  • 服务器配置应包括您的 ssl 证书和位于服务器上的密钥的路径。

推荐阅读