jilenloa/echo-socketcluster-broadcast-driver

Laravel socketcluster 广播器。它与 Laravel Echo 客户端兼容

dev-master 2023-03-07 01:16 UTC

This package is auto-updated.

Last update: 2024-09-07 04:32:30 UTC


README

Build Status Total Downloads

它支持 PHP 7.1+ 和 Laravel 5.8+

在使用此库之前,您首先需要设置一个名为 "echo-socketcluster-server" 的服务器。它与默认的 "laravel-echo-server" 类似,但在您运行 vendor:publish 命令时安装。请参阅以下内容。

php artisan vendor:publish --tag=echosocketcluster

使用 SocketCluster 的 Echo 兼容服务器

echo-socketcluster-server 包是使用 SocketCluster 创建的,它是此存储库的一部分,您可以将它复制到您选择的任何文件夹。它包含自己的 package.json,因此您需要在文件夹中运行 npm install 命令。

有关 SocketCluster 的更多信息,请访问 https://socketcluster.io/

Laravel 安装

使用 Composer 安装包

composer require jilenloa/echo-socketcluster-broadcast-driver:dev-master

将以下内容添加到您的 broadcasting.php 文件中。

'echosocketcluster' => [
    'driver' => 'echosocketcluster',
],

将以下配置添加到您的 .env 文件中。将 BROADCAST_DRIVER 的当前设置更新如下

BROADCAST_DRIVER=echosocketcluster #"redis" is also compatible with this broadcast server just like the default laravel-echo

ECHO_SC_HOST=localhost:8001
ECHO_SC_TOKEN=echo-server-token
REDIS_KEY_PREFIX=laravel_ # just like you would set in laravel-echo-server.json
SOCKETCLUSTER_PORT=8001
SOCKETCLUSTER_WS_ENGINE=uws # defaule engine is "ws"
SOCKETCLUSTER_HTTP_TOKEN=echo-server-token

如果您尚未这样做,可以使用以下命令发布配置和 echo-socketcluster-server 文件

php artisan vendor:publish --tag=echosocketcluster

然后您可以使用 composer 更新包

composer update

Laravel Echo 客户端

npm install laravel-echo
npm install laravel-echo-connector-socketcluster

以下是 Laravel Echo 客户端的一个示例用法。

import Echo from "laravel-echo";
import SocketClusterConnector from "laravel-echo-connector-socketcluster";
window.socketClusterClient = require('./socketcluster-client');

let echo = new Echo({
    client: socketClusterClient,
    broadcaster: SocketClusterConnector,
      auth: {
        headers: {
            //add custom headers here, useful for JWT authentication
        },
        hostname: 'localhost:8001', //laravel host to authorize channels. this is sometimes optional
      },
      socketcluster: {
            hostname: 'localhost',
            port: 8001
        }
    });

有关 laravel-echo 的更多信息,请访问 https://laravel.net.cn/docs/broadcasting

最后

  • 在测试之前,请确保您已启动 echo-socketcluster-server。

如果您遇到任何挑战,请毫不犹豫地在此处提交问题 here