简体   繁体   English

如何使用laravel-websockets在公共频道中注册网址?

[英]How to make an url available for registration in a public channel using laravel-websockets?

How to make an url to any user listen to a websocket channel, like wss://stream.exemplo.com:9443/ws/channel@event ? 如何为任何用户创建一个url来收听websocket频道,比如wss://stream.exemplo.com:9443/ws/channel@event

I'm using the beyondcode/laravel-websockets package and I can consume the channels normally following the docs, but only with a javascript library like echo. 我正在使用beyondcode / laravel-websockets包,我可以按照文档正常使用频道,但只能使用像echo这样的javascript库。 My doubts are not about the protocol or how to listen it with Echo, it's just about how to make the part of /channel@event public available to anyone start to listen to it in a website like https://websocket.org/echo.html without any type of authentication. 我的疑问不是关于协议或如何用Echo听它,它只是关于如何让/channel@event的部分公开可供任何人开始在https://websocket.org/echo这样的网站上收听它.html没有任何类型的身份验证。

I can see in they code and in the generated logs that they always require an appId (cause the package is multi-tenancy). 我可以在他们的代码和生成的日志中看到他们总是需要appId(导致包是多租户)。

The FetchChannelController find the channel by appId and channelName FetchChannelControllerappIdchannelName查找channelName

class FetchChannelController extends Controller
{
    public function __invoke(Request $request)
    {
        $channel = $this->channelManager->find($request->appId, $request->channelName);

        if (is_null($channel)) {
            throw new HttpException(404, "Unknown channel `{$request->channelName}`.");
        }

        return $channel->toArray();
    }
}
public function find(string $appId, string $channelName): ?Channel
{
    return $this->channels[$appId][$channelName] ?? null;
}

public function getChannels(string $appId): array
{
    return $this->channels[$appId] ?? [];
}

But I expect a way that any user without anything more than channel@event can subscribe to the channel in something like https://websocket.org/echo.html 但是我希望任何没有任何超过channel @ event的用户都可以通过https://websocket.org/echo.html订阅这个频道。

[EDIT] [编辑]

I expect a way to make an endpoint that the website https://websocket.org/echo.html can listen to it. 我希望有一种方法可以建立一个端点,网站https://websocket.org/echo.html可以听取它。 I have hundreds of events been broadcasted to a channel "news" for example. 例如,我有数百个事件被广播到频道“新闻”。 So how (using an endpoint, not the channel name in a javascript library) the https://websocket.org/echo.html will start to listen to this channel? 那么如何(使用端点,而不是javascript库中的频道名称) https://websocket.org/echo.html将如何开始收听此频道? Without knowledge of any appId of laravel-websockets, just with the channel name. 不知道laravel-websockets的任何appId,只需使用频道名称。

One thing is a channel id and another one a user id . 一件事是渠道 id ,另一件是用户 id The channel and app ids will be used for Laravel-Websockets to identify to wich app and channel the server will emit/listen events. 通道和应用程序ID将用于Laravel-Websockets以识别服务器将发出/收听事件的应用程序和频道。 The user id is helpful to check if the user is identified to access a protected (private) channel. 用户ID有助于检查用户是否被识别为访问受保护(私有)频道。

If you want to listen to every event, create a general channel and then, in your website subscribe it to this public general channel to listen to your desired events fired in this general channel. 如果您想收听每个活动,请创建一个通用频道,然后在您的网站中订阅此公共通用频道,以收听您在此常规频道中发布的所需活动。


Update 更新

Given your actual requirement 鉴于您的实际要求

As I said in my comment, you could manage the url string to inject the to your Laravel Echo (or your WS front-end library) in order to make it listen to your events. 正如我在评论中所说,您可以管理url字符串以将其注入Laravel Echo(或您的WS前端库),以使其能够监听您的事件。 Given an endpoint like this: 给定这样的端点:

wss://stream.exemplo.com:9443/ws/channel@event WSS://stream.exemplo.com:9443 / WS /信道@事件

You could: 你可以:

$string = 'wss://stream.exemplo.com:9443/ws/channel@event';
$segments = explode('/', $string);
/** ['wss:', '', 'stream.exemplo.com:9443', 'ws', 'channel@event'] */

$url = explode(':', $segments[2]);
/** ['stream.exemplo.com', '9443'] */

$event_data = explode('@', $segments[4]);
/** ['channel', 'event'] */

So you have everything to connect to your front-end library ( appId aside, as you said you will handle this on your end): 因此,您可以将所有内容连接到您的前端库( appId旁边,正如您所说,您将在最后处理此问题):

$wsHost = $url[0];  // 'stream.exemplo.com'
$wsPort = $url[1];  // '9443'
$channel = $event_data[0]; // 'channel'
$event = $event_data[1]; // 'event'

PS: Here I'm using the explode() php's helper. PS:我在这里使用explode() php的助手。

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

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