如何使用AspNetCore SignalR创建集线器代理 [英] How to create Hub Proxy using AspNetCore SignalR

查看:183
本文介绍了如何使用AspNetCore SignalR创建集线器代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以从AspNet SignalR知道与该代码等效的AspNetCore SignalR吗?

Anyone know the AspNetCore SignalR equivalent of this code from AspNet SignalR?

Task.Factory.StartNew(() =>
{
    ////  var clients = Hub.Clients<RealTimeNotificationHub>();
    var connection = new HubConnectionContext("https://demohouse.go.ro:96/");
    var notificationmessag = new AlertMessage();
    notificationmessag.Content = "New message from " + device.Name + " <b>" +
                                 text + " </b>";
    notificationmessag.Title = alertType.Name;
    var myHub = connection.CreateHubProxy("realTimeNotificationHub");
    connection.Start().Wait();
    object[] myData = { notificationmessag.Title, notificationmessag.Content };
    myHub.Invoke("sendMessage", myData).Wait();
    connection.Stop();
});

推荐答案

添加Nuget包Microsoft.AspNetCore.SignalR.Client并尝试以下代码.我可以自由使用您发布信息上的数据,并将其转换为.Net Core版本.

Add Nuget package Microsoft.AspNetCore.SignalR.Client and try the below code. I took the liberty to use your data on your post and translate it to the .Net Core version.

//I'm not really sure how your HUB route is configured, but this is the usual approach.

var connection = new HubConnectionBuilder()
                .WithUrl("https://demohouse.go.ro:96/realTimeNotificationHub") //Make sure that the route is the same with your configured route for your HUB
                .Build();

var notificationmessag = new AlertMessage();
notificationmessag.Content = "New message from " + device.Name + " <b>" +
                                         text + " </b>";
notificationmessag.Title = alertType.Name;

connection.StartAsync().ContinueWith(task => {
    if (task.IsFaulted)
    {
        //Do something if the connection failed
    }
    else
    {
        //if connection is successfull, do something
        connection.InvokeAsync("sendMessage", myData);

    }).Wait();

您可以使用其他方法执行异步任务.

You can use a different approach on executing async tasks.

注意:您的集线器还应使用SignalR的.Net Core软件包(Microsoft.AspNetCore.SignalR),以使其正常工作(根据我的经验).

Note: Your Hub should be also using the .Net Core package for SignalR (Microsoft.AspNetCore.SignalR) in order for this to work (based on my experience).

这篇关于如何使用AspNetCore SignalR创建集线器代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆