简体   繁体   中英

How to send new/updated data to all clients on SignalR for NET Core 2.2

I'm not too familiar with signalr2 on asp.net-core pardon me, am trying to create a POC on how to implement a real-time CRUD application with admin and client using signalr2, but am having issues with signalr data push, I keep getting NullReferenceException: Object reference not set to an instance of an object. on this line await Clients.All.SendAsync("BroadcastData", data);

Below is how I setup my Hub using some online examples I looked-up:

public class OddPublisher : Hub
    {

        private readonly IOddServices _oddService;

        public OddPublisher(IOddServices oddService)
        {
            _oddService = oddService;
        }

        public async Task BroadcastData()
        {
            var data = _oddService.ClientQueryOdds();
            await Clients.All.SendAsync("BroadcastData", data); //breaks here
        }

    }

and this is triggered by admin, on submiting and saving the data sucessfully I call the BroadcastData()

    public class BaseController : Controller
    {
        public IOddServices _oddService;
        public readonly OddPublisher _publisher;
        public BaseController(IOddServices oddService, )
        {
            _oddService = oddService;
            _teamService = teamService;

            _publisher = new OddPublisher(oddService);
        }


        [HttpPost]
        public async Task<IActionResult> odd_entry(CreateOdd dto)
        {
            //somecode here...

            var results = _validator.Validate(dto);

            if(!results.IsValid)
            {
                results.AddToModelState(ModelState, null);

                return View(dto);
            }

            _oddService.CreateOddAndTeam(dto);

            await _publisher.BroadcastData(); //Breaks

            return RedirectToAction(nameof(index));
        }
    }

Folled all the instructions as adviced in Microsoft Asp.Net Core Signalr Documentation, my Startup has all required sevices added.

here is the client and the JS file,

"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/oddPublisher").build();

connection.on("BroadcastData", data => {

    console.table(data);
    //getAll();
})


connection.start().then(function () {
    getAll();
}).catch(function (err) {
    return console.error(err.toString());
});


function getAll() {
    var model = $('#dataModel');
    $.ajax({
        url: '/home/GetLatestOddData',
        contentType: 'application/html ; charset:utf-8',
        type: 'GET',
        dataType: 'html',
        success: function (result) { model.empty().append(result); }
    });
}

Need some help here guys, I still don't know what am doing wrong, I'll really appreciate if I can get any help on this.

Thank you in advance.

事实证明我需要将IHubContext注入我的集线器才能访问客户端。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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