SignalR.Client.Generic
1.0.1
dotnet add package SignalR.Client.Generic --version 1.0.1
NuGet\Install-Package SignalR.Client.Generic -Version 1.0.1
<PackageReference Include="SignalR.Client.Generic" Version="1.0.1" />
paket add SignalR.Client.Generic --version 1.0.1
#r "nuget: SignalR.Client.Generic, 1.0.1"
// Install SignalR.Client.Generic as a Cake Addin #addin nuget:?package=SignalR.Client.Generic&version=1.0.1 // Install SignalR.Client.Generic as a Cake Tool #tool nuget:?package=SignalR.Client.Generic&version=1.0.1
SignalR.Client.Generic
Step 1
Create 'Core' project, which will contains 2 folders:
- HubEvents - contains interfaces, which describe server events.
- HubModels - contains interfaces, which describe server methods.
For current example folder 'HubEvents' contains next interface:
public interface IChatHubEvents
{
Task OnNewChatMessage(string userName, string message);
}
And folder 'HubModels' contains next interfaces:
public interface IChatHubModel
{
Task SendMessage(string userName, string message);
}
public interface ISecondHubModel
{
Task<IEnumerable<int>> GetRandomInts(int count, int min, int max);
}
Step 2
Create server and hub.
Hub must implement next interfaces: IChatHubEvents, ISecondHubModel
In example Hub was created as a partial class:
public partial class MainHub : Hub<IChatHubEvents>
{
}
public partial class MainHub : IChatHubModel
{
public Task SendMessage(string userName, string message)
{
return Clients.All.OnNewChatMessage(userName, message);
}
}
public partial class MainHub : ISecondHubModel
{
static Random rn = new Random();
public Task<IEnumerable<int>> GetRandomInts(int count, int min, int max)
{
return Task.FromResult(Enumerable.Range(0, count).Select(x => rn.Next(min, max)));
}
}
Next step: add signalr to services
> Startup.cs
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddSignalR();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<MainHub>("/mainhub");
});
}
}
Step 3
Create ProxyHubConnection:
ProxyHubConnection hubConnection = new ProxyHubConnection(x => x.WithUrl("https://localhost:5001/mainhub").WithAutomaticReconnect());
ProxyHubConnection must not implement interfaces: IChatHubModel, ISecondHubModel, but must implement events (IChatHubEvents) in other classes.
ProxyHubConnect you can inherit and override methods: InvokeCoreAsync, InvokeCoreAsync<T>
Next step: implement server events
class ChatEvents : IChatHubEvents
{
public Task OnNewChatMessage(string userName, string message)
{
Console.WriteLine($"New message: {userName} => {message}");
return Task.CompletedTask;
}
}
Next step: register events in ProxyHubConnection
hubConnection.RegisterCallbacks(new ChatEvents());
Next step: create two HubProxy
var chatProxy = hubConnection.GetHubProxy<IChatHubModel>();
var secondProxy = hubConnection.GetHubProxy<ISecondHubModel>();
Now we ready to connect to server (proxy may create after/before connection)
await hubConnection.Connection.StartAsync();
Well, we connected to server. Now we can invoke server method over proxy:
var randInts = await secondProxy.GetRandomInts(10, 0, 10);
foreach (var item in randInts)
{
Console.WriteLine($"Random int: {item}");
}
It works!
Let's try to send a message to chat:
Console.Write("Input name: ");
var name = Console.ReadLine();
while (true)
{
var msg = Console.ReadLine();
await chatProxy.SendMessage(name, msg);
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Castle.Core (>= 4.4.1)
- Microsoft.AspNetCore.SignalR.Client (>= 5.0.0)
- System.Text.Encodings.Web (>= 5.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.