0

I'm trying to use SignalR on Mvc, and I need help. Server need send only infromation to client and client need show this value on page. Client dont need send some invoke for server it must be made automatically.

Can someone help. Thanks.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

I create hub class and call hub from controller with a hub dependency, and create javascript file but i didnt see value on page.

Jason Pan
  • 15,263
  • 1
  • 14
  • 29
jozo
  • 1
  • 1
    Welcome to Stack Overflow. What is your actual question? We can help you with focused questions, but Stack Overflow is not meant for multi-step debugging of programs. Please read [ask]. Also [don't post images of code](https://meta.stackoverflow.com/q/285551/6717178). – JHBonarius Aug 16 '23 at 10:20
  • Please see this link for help. https://stackoverflow.com/questions/12188029/how-do-i-send-messages-from-server-to-client-using-signalr-hubs – SoftwareDveloper Aug 16 '23 at 14:18

1 Answers1

0

I don't know why the _hub.Clients.All.SendSync not works in your side, it should be work. But you also could try to define the function inside the Hub class and invoke it in your controller. Here is the sample, I have tested it.

And you want server send the message to clients automaticly, we could use Background service. Here is the sample. You can modify it according your needs. For more details, you can check my previous answer.

using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SignalRMiddleawre.Hubs;

namespace BackgroundService
{
    public class TimedHostedService : IHostedService, IDisposable
    {
        private int executionCount = 0;
        private readonly ILogger<TimedHostedService> _logger;
        private readonly IHubContext<MainHub> _hubcontext;
        private Timer? _timer = null;

        public TimedHostedService(IHubContext<MainHub> hubcontext, ILogger<TimedHostedService> logger)
        {
            _hubcontext = hubcontext;
            _logger = logger;
        }

        public  Task StartAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Timed Hosted Service running.");
            _hubcontext.Clients.All.SendAsync("Chat_ReceiveMessage", "[System - Background Service ]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + "Timed Hosted Service running.");
            _timer = new Timer(DoWork, null, TimeSpan.Zero,
                TimeSpan.FromSeconds(3));

            return Task.CompletedTask;
        }

        private void DoWork(object? state)
        {
            var count = Interlocked.Increment(ref executionCount);

            _logger.LogInformation(
                "Timed Hosted Service is working. Count: {Count}", count);
            _hubcontext.Clients.All.SendAsync("Chat_ReceiveMessage", "[System - Background Service ]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + "DoSomethingAsync method triggered.  Count: "+count+"");
        }

        public  Task StopAsync(CancellationToken stoppingToken)
        {
            _logger.LogInformation("Timed Hosted Service is stopping.");
             _hubcontext.Clients.All.SendAsync("Chat_ReceiveMessage", "[System - Background Service ]", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + "Timed Hosted Service is stopping.");

            _timer?.Change(Timeout.Infinite, 0);

            return Task.CompletedTask;
        }

        public void Dispose()
        {
            _timer?.Dispose();
        }
    }
}
Jason Pan
  • 15,263
  • 1
  • 14
  • 29