Home >Backend Development >C++ >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); } } } }
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(); } } }
Additional Information
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!