Как создать Hub Proxy с помощью AspNetCore SignalR - PullRequest
0 голосов
/ 28 сентября 2018

Кто-нибудь знает AspNetCore SignalR, эквивалентный этому коду из 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();
});

1 Ответ

0 голосов
/ 28 сентября 2018

Добавьте пакет Nuget Microsoft.AspNetCore.SignalR.Client и попробуйте следующий код.Я позволил себе использовать ваши данные в вашем посте и перевести их на версию .Net Core.

//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();

Вы можете использовать другой подход при выполнении асинхронных задач.

Примечание.Хаб должен также использовать пакет .Net Core для SignalR (Microsoft.AspNetCore.SignalR), чтобы это работало (исходя из моего опыта).

...