static async Task Main(string[] args) { Console.BackgroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Hello Signal-R sender!"); const string url = "http://localhost:5000/hubs/customers"; // dotnet add package Microsoft.AspNetCore.SignalR.Client HubConnection connection = new HubConnectionBuilder() .WithUrl(url) .Build(); Console.WriteLine("Connecting..."); await connection.StartAsync(); Console.WriteLine("Connected."); CustomerFaker customerFaker = new CustomerFaker(new AddressFaker()); var customers = customerFaker.GenerateForever(); foreach (var customer in customers) { await connection.SendAsync("CustomerAdded", customer); Console.WriteLine($"Sent {customer.FirstName} {customer.LastName}"); await Task.Delay(TimeSpan.FromSeconds(1)); } Console.ResetColor(); }
// dotnet add package Microsoft.AspNetCore.SignalR.Client static async Task Main(string[] args) { Console.BackgroundColor = ConsoleColor.DarkBlue; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Hello Signal-R Sender!"); string url = "http://localhost:5000/signalr/customers"; HubConnection connection = new HubConnectionBuilder() .WithUrl(url) .Build(); // HubConnection<ICustomerServer> // connection.OnYouHaveGotMessage(customer => => Console.WriteLine($"Received {customer.FirstName} {customer.LastName}")); connection.On <Customer>(nameof(ICustomerClient.YouHaveGotMessage), customer => Console.WriteLine($"Received {customer.FirstName} {customer.LastName}")); connection.On(nameof(ICustomerClient.Pong), () => Console.WriteLine("Pong")); Console.WriteLine("Connecting..."); await connection.StartAsync(); string groupName = string.Format("Grupa {0}", connection.ConnectionId[0] % 3); Console.WriteLine($"Connected {groupName}."); await connection.SendAsync("Ping"); CustomerFaker customerFaker = new CustomerFaker(); var customers = customerFaker.GenerateForever(); foreach (var customer in customers) { Console.WriteLine($"Send {customer.FirstName} {customer.LastName}"); await connection.SendAsync(nameof(ICustomerServer.SendCustomerAdded), customer); // await connection.SendAsync("SendCustomerAddedToGroup", customer, groupName); await Task.Delay(TimeSpan.FromMilliseconds(100)); } Console.WriteLine("Press any key to exit."); Console.ReadKey(); Console.ResetColor(); }
// >= C# 7.1 static async Task Main(string[] args) { const string url = "http://localhost:5000/signalr/customers"; Console.BackgroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.White; Console.WriteLine("Hello Signal-R Sender!"); // dotnet add package Microsoft.AspNetCore.SignalR.Client HubConnection connection = new HubConnectionBuilder() .WithUrl(url) .WithAutomaticReconnect() .Build(); connection.Reconnected += connectionId => { if (connection.State == HubConnectionState.Connected) { // TODO: Przesylanie danych z kolejki } return(Task.CompletedTask); }; connection.Closed += error => { if (connection.State == HubConnectionState.Disconnected) { // TODO: Powiadomienie użytkownika } return(Task.CompletedTask); }; Console.WriteLine("Connecting..."); await connection.StartAsync(); Console.WriteLine("Connected."); CustomerFaker customerFaker = new CustomerFaker(); // Customer customer = customerFaker.Generate(); var customers = customerFaker.GenerateForever(); foreach (var customer in customers) { Console.WriteLine($"Sending {customer.FirstName} {customer.LastName}"); await connection.SendAsync("SendAddedCustomer", customer); Console.WriteLine($"Sent."); await Task.Delay(TimeSpan.FromSeconds(1)); } Console.WriteLine("Press any key to exit."); Console.ReadKey(); }