简体   繁体   中英

How to push data to clients from a SignalR hub in C#

I am using the code from the MS sample:

        public Task PushMessage(string Message)
        {
            var T = Clients.All.SendAsync(Message);
            Console.WriteLine("I'm here");
            return T;
        }

but the writeline call is never reached. So do I need to check that at least one client is connected before pushing messages?

what I am trying to achieve is the following: I have a lot of small messages being produced that need to be pushed to the clients connected to the hub, if any. so the messages are produced no matter what and if clients are connected, they should receive it.

right now, I've put a SendMessage call in the event that is generated when new data is being built, but this just results in the callback being held because SendMessage doesn't return.

the behavior I am looking for is: if there are clients, they get the message, if there are no clients, the message is discarded.

how can I achieve this?

I think I may be misunderstanding how SignalR works and I would really like to find an example C# <-> C# because every single example on the web is web/js centric.

Walk through some of the sample code for console applications in their GitHub .

SignalR will only broadcast if there are connections to be broadcast to. That's part of the point of this, so you don't have to manage that aspect. You can certainly poll for data or get it however you like, but when you tell it to send SignalR will check to see if there is anyone to send it to.

As far as the C# to C# vs C# to web/js, yes, you are thinking about that wrong. If it's a console app or web/js it's a client. The communication is client to server and server(hub) to client. It does not matter if it is a console app or web/js or any other type of client.

Here's a sample of a console app I use for demo purposes that allows me to chat back and forth between console and a web page or web page to console through the hub:

        private static void Main(string[] args)
    {
        try
        {

            var connection = new HubConnection("http://localhost:7132/");
            IHubProxy hub = connection.CreateHubProxy("ChatHub");

            hub.On<string, string>("broadcastMessage", (name, message) => { Console.Write(name + ": "); Console.WriteLine(message); });
            connection.Start().Wait();
            hub.Invoke("Notify", "Console app", connection.ConnectionId);
            string msg = null;

            while ((msg = Console.ReadLine()) != null)
            {
                hub.Invoke("Send", "Console app", msg).Wait();
            }

        }
        catch (Exception e)
        {
            Console.WriteLine("Error" + e.Message);
        }

    }

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