如何扩展laravel websockets服务器(BeyondCode)以直接触发larave

我研究了很长时间,据我了解,从我的 laravel 基础应用程序来看,BeyondCode 由 websockets 服务器触发的 websocket 事件只是一种消息传递方式

消息可以从 laravel 基础应用程序发送到客户端(客户端对消息做出反应)。来自其他 websocket 客户端的事件发送到 websocket 服务器并且我的 laravel 基础应用程序对其做出反应(触发事件)尚未实现。

要触发我的 laravel 基础应用程序上的事件,可以使用 webhooks。这是对通用生成路由的标准 http 请求。

我理解得对吗?

如果我无法接收消息/事件,为什么我的 Laravel 基础应用程序有一个由 WebSocket 服务器提供服务的服务器?在这种情况下,为什么我应该只使用一个方向的 websocket?

我还是一个 laravel 新手。我想更改/覆盖一些类,查看BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel,以便在正确的套接字发送正确的事件时使某些通道触发事件。

如何用我BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel自己的类覆盖该类而不弄乱 BeyondCode 的 websocket 包?我不想碰供应商的东西。

websocket 服务器是完全独立的还是我可以从服务器类中访问我的基本应用程序?如果我这样做,我需要注意什么?


元芳怎么了
浏览 103回答 3
3回答

繁华开满天机

您可以将库复制到项目目录中并在以下位置设置路径composer.json:    "repositories": [        {            "type": "path",            "url": "path-to-the-copy-of-the-library"        }    ],或者您可以创建另一个存储库并分叉该库并再次编辑composer.json:    "repositories": [        {            "type": "vcs",            "url": "URL-to-your-repository"        }    ],确保您的库符合要求:    "require": {        "beyondcode/laravel-websockets": "*",    }然后运行composer install

当年话下

需要一些时间来解决这个问题,但现在我找到了一个答案,最小化了供应商代码的覆盖。感兴趣的文件位于 Channels 文件夹下方:|-- laravel-websockets|&nbsp; &nbsp;|-- src|&nbsp; &nbsp;|&nbsp; &nbsp;|-- WebSockets|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|-- Channels|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|-- ChannelManagers|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|-- ArrayChannelManager.php|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|-- Channel.php|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|-- ChannelManager.php|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|-- PresenceChannel.php|&nbsp; &nbsp;|&nbsp; &nbsp;|&nbsp; &nbsp;|-- PrivateChannel.php在 中Channel.php,PresenceChannel.php所有PrivateChannel.php推送服务器相关事件均得到处理。例如,订阅和发送消息。该类ArrayChannelManager控制 PrivateChannel、PresenceChannel 和 Channel 使用的类。Laravel-websockets 使您可以为您的项目使用自己的通道管理器,在 中指定/config/websockets.php -> 'channel_manager'。为了将 Laravel 事件添加到推送服务器事件,我重载ArrayChannelManager了PrivateChannel(如果需要,您也可以重载其他通道)并添加了 Laravel 排队事件。在/config/websockets.php我使用我的重载类作为channel_manager。/config/websockets.php:'channel_manager' => \App\Lib\Overwrite\LaravelWebsockets\ArrayChannelManager::class,我重载的 ArrayChannelManager:<?phpnamespace App\Lib\Overwrite\LaravelWebsockets;use BeyondCode\LaravelWebSockets\WebSockets\Channels\Channel;use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManagers\ArrayChannelManager as ArrayChannelManagerBase;use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;use App\Lib\Overwrite\LaravelWebsockets\PrivateChannel;use Illuminate\Support\Arr;use Illuminate\Support\Str;use Ratchet\ConnectionInterface;class ArrayChannelManager extends ArrayChannelManagerBase{&nbsp; &nbsp; protected function determineChannelClass(string $channelName): string&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (Str::startsWith($channelName, 'private-')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return PrivateChannel::class;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (Str::startsWith($channelName, 'presence-')) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return PresenceChannel::class;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return Channel::class;&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;};在我的重载PrivateChannel文件中,我将 a 挂钩MyPusherLaravelEvent到推送器订阅事件:<?phpnamespace App\Lib\Overwrite\LaravelWebsockets;use BeyondCode\LaravelWebSockets\WebSockets\Channels\PrivateChannel as PrivateChannelBase;use Ratchet\ConnectionInterface;use stdClass;class PrivateChannel extends PrivateChannelBase{&nbsp; &nbsp; public function subscribe(ConnectionInterface $connection, stdClass $payload)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; parent::subscribe($connection, $payload);&nbsp; &nbsp; &nbsp; &nbsp; \App\Events\MyPusherLaravelEvent::dispatch($this->channelName);&nbsp; &nbsp; }}使用 Laravel 排队事件将事件处理与推送服务器分开非常重要。https://laravel.com/docs/8.x/events#queued-event-listeners您还可以将事件挂钩到Channel::broadcast...函数,然后解析内容以对服务器的某些客户端命令做出反应。以通用方式重载函数,因此可以更新供应商代码,而无需用重载破坏它。

ibeautiful

你不需要覆盖任何东西,你的 laravel 后端将向 websockets 服务器发出一个事件,websockets 服务器将在相应的通道中将事件发送回你的前端
打开App,查看更多内容
随时随地看视频慕课网APP