繁体   English   中英

Laravel echo服务器无法在生产服务器中运行

[英]Laravel echo server not working in production server

我有socket.io作为一个广播员与laravel回声的问题。

我试过了什么:

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服务器配置,其中所有路径都是正确的。 我在我的localhost上测试了相同的文件,一切正常:

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'];
    }

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

来自docs

使用SSL运行

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

如果您看到用户加入频道,则很可能正确设置。 您只需要为您的console.log(notification)呼叫广播通知,因为您专门在用户通道上调用通知功能。 有关详细信息,请参阅https://laravel.com/docs/5.6/broadcasting#notifications

通知广播:

//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());

编辑:

此外,请确保您的用户模型中具有Notifiable特征:

class User extends Authenticatable
{
    use Notifiable;

    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM