信号-R是否由NET MAUI支持?

发布于 2025-01-22 23:15:09 字数 165 浏览 0 评论 0原文

我在Blazor Server应用程序中使用了Signal-R,但是我看不到在.NET MAUI中实现它的方法。显然,这种环境还不支持Signal-R,但我可能错了。

如果是这样,我如何将ihubContext&xhub>声明为mauipragram.cs中的服务?

I've used Signal-R in a Blazor Server app, but I don't see a way to implement it in .NET MAUI. Apparently this environment doesn't support Signal-R yet, but I might be wrong.

If it is, how do I declare an IHubContext<XHub> as a service in MauiProgram.cs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惜醉颜 2025-01-29 23:15:09

是的,信号r工作

设置:

Install Microsoft.AspNetCore.SignalR.Client

在您的ViewModel或页面中添加:

using Microsoft.AspNetCore.SignalR.Client;

声明:
私有Readonly HubConnection _hubConnection;

在页面或View -Modal创建/构造函数上:

_hubConnection = new HubConnectionBuilder().WithUrl(HUB_URL).WithAutomaticReconnect().Build();

        _hubConnection.On<YourModel>("YOUR_EVENT", (item) =>
        {
            MainThread.BeginInvokeOnMainThread(async () =>
            {
                // Do a thing
            });
        });

必要时初始化连接:

// Start signalr to get updates
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Task.Run(async () =>
                    {
                        try
                        {
                            var connected = _hubConnection.State == HubConnectionState.Connected;
                            if (!connected)
                            {
                                await _hubConnection.StartAsync();
                                await _hubConnection.InvokeAsync("Subscribe", YOUR_IDENTIFIER);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    });
                });

URL类似:

SiteUrl + "hubs/Yourhub";

您的网站具有类似的内容:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<App.SignalR.Hubs.FilesHub>("hubs/Yourhub");

            });

Yes, Signal R Works

Setup:

Install Microsoft.AspNetCore.SignalR.Client

In your viewmodel or Page add:

using Microsoft.AspNetCore.SignalR.Client;

Declare:
private readonly HubConnection _hubConnection;

On Page or ViewModal creation/constructor:

_hubConnection = new HubConnectionBuilder().WithUrl(HUB_URL).WithAutomaticReconnect().Build();

        _hubConnection.On<YourModel>("YOUR_EVENT", (item) =>
        {
            MainThread.BeginInvokeOnMainThread(async () =>
            {
                // Do a thing
            });
        });

Initialise the connection when required:

// Start signalr to get updates
                MainThread.BeginInvokeOnMainThread(() =>
                {
                    Task.Run(async () =>
                    {
                        try
                        {
                            var connected = _hubConnection.State == HubConnectionState.Connected;
                            if (!connected)
                            {
                                await _hubConnection.StartAsync();
                                await _hubConnection.InvokeAsync("Subscribe", YOUR_IDENTIFIER);
                            }
                        }
                        catch (Exception ex)
                        {
                        }
                    });
                });

The url is something like:

SiteUrl + "hubs/Yourhub";

Where your site has something like:

app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<App.SignalR.Hubs.FilesHub>("hubs/Yourhub");

            });
凉月流沐 2025-01-29 23:15:09

我自己没有尝试过(今年晚些时候会做),但是根据这个项目: https ://github.com/matt-goldman/maui-chat 应该支持它。

I haven't tried it myself (will do later this year), but according to this project: https://github.com/matt-goldman/maui-chat it should be supported.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文