Home > Backend Development > C++ > How to Send Messages Between Console Applications Using SignalR?

How to Send Messages Between Console Applications Using SignalR?

Susan Sarandon
Release: 2025-01-04 16:13:41
Original
853 people have browsed it

How to Send Messages Between Console Applications Using SignalR?

Send Messages with SignalR in a Console App

SignalR enables real-time communication between a server and connected clients. To utilize SignalR, you'll need to install SignalR.Hosting.Self on the server application and SignalR.Client on the client application through NuGet.

Server Console App

using System;
using SignalR.Hubs;

namespace SignalR.Hosting.Self.Samples {
    class Program {
        static void Main(string[] args) {
            string url = "http://127.0.0.1:8088/";
            var server = new Server(url);

            server.MapHubs();
            server.Start();

            Console.WriteLine("Server running on {0}", url);

            while (true) {
                var key = Console.ReadKey(true);
                if (key.Key == ConsoleKey.X) {
                    break;
                }
            }
        }

        [HubName("CustomHub")]
        class MyHub : Hub {
            public string Send(string message) {
                return message;
            }

            public void DoSomething(string param) {
                Clients.addMessage(param);
            }
        }
    }
}
Copy after login

Client Console App

using System;
using SignalR.Client.Hubs;

namespace SignalRConsoleApp {
    internal class Program {
        static void Main(string[] args) {
            var connection = new HubConnection("http://127.0.0.1:8088/");
            var myHub = connection.CreateHubProxy("CustomHub");

            connection.Start().Wait();
            Console.WriteLine("Connected");

            myHub.Invoke<string>("Send", "HELLO World ").Wait();
            Console.WriteLine("Message sent.");

            myHub.On<string>("addMessage", param => Console.WriteLine(param));

            myHub.Invoke("DoSomething", "I'm doing something!!!").Wait();

            Console.Read();
            connection.Stop();
        }
    }
}
Copy after login

Additional Information

  • Assign a custom hub name using [HubName("CustomName")] but note that using a standard hub name might cause compatibility issues.
  • Run both projects as administrators.
  • If you receive an "Unknown hub" error, ensure that both the hub name and the client proxy name match.

The above is the detailed content of How to Send Messages Between Console Applications Using SignalR?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template