Skip to content

Latest commit

 

History

History
169 lines (122 loc) · 5.81 KB

README.md

File metadata and controls

169 lines (122 loc) · 5.81 KB

SignalR.EasyUse

SignalR

GitHub last commit GitHub code size in bytes

GitHub search hit counter Nuget Nuget Nuget

What is it?

SignalR.EasyUse is a framework that helps eliminate contract mismatch errors when developing client and server applications in C# using SignalR.

How to use it?

Create an Interface

  1. Create a new class library project in your solution that will contain contracts.
  2. Add the NuGet package to this project:
Install-Package SignalR.EasyUse.Interface

Define Server Methods

  1. Create an interface with server methods. The interface method names will be used as identifiers, and the parameters will be passed directly. The method's return type must be Task or Task<TResult>.
  2. Optionally, the interface can inherit from SignalR.EasyUse.Interface.IServerMethods to easily distinguish server methods, though this is not required.

Example:

public interface IChatHub: IServerMethods
{
    Task SendMessage(string user, string message);
    Task<List<User>> Login(string name, byte[] photo);
}

Define Client Methods

  1. Define client methods (those called by the server) by creating a class for each method. The class name will be used as an identifier, and all public properties of the class will be passed as parameters in the order they are declared.
  2. Optionally, these classes can inherit from SignalR.EasyUse.Interface.IClientMethod, but it’s not mandatory.

Example:

public class ReceiveMessage: IClientMethod
{
    public string User { get; set; }
    public string Message { get; set; }
}

Use Interfaces in the Server Project

  1. Add a reference to the project containing the contracts.
  2. Add the NuGet package to the server project:
Install-Package SignalR.EasyUse.Server
  1. Implement your server interface. Since the interface defines the contract, your hub will comply with it.
  2. Use strongly typed client calls with the extension method to send messages:
async Task SendAsync<T>(this IClientProxy clients, T payload)

Example:

public class ChatHub : Hub, IChatHub
{
    public async Task SendMessage(string user, string message)
    {
        await Clients.All.SendAsync(new ReceiveMessage
        {
            User = user,
            Message = message,
        });
        
        // Equivalent native call
        await Clients.All.SendAsync("ReceiveMessage", user, message);
    }
}

Use Interfaces in the Client Project

  1. Add a reference to the project containing the contracts.
  2. Add the NuGet package to the client project:
Install-Package SignalR.EasyUse.Client
  1. Create a connection to the hub as usual:
_connection = new HubConnectionBuilder()
                .WithUrl("http://localhost:53353/ChatHub")
                .Build();
  1. Create a dynamic proxy for the hub based on the interface:
var hub = _connection.CreateHub<IChatHub>();
  1. Use the proxy to call server methods:
var users = await hub.Login(UserName, Photo);
await hub.SendMessage(UserName, MessageText);

Without EasyUse, the same call would look like this:

var users = await connection.InvokeCoreAsync<List<User>>("Login", new object[] { UserName, Photo });
await _connection.InvokeAsync("SendMessage", UserName, MessageText);
  1. To subscribe to messages from the server, use the extension method:
void Subscribe<T>(this HubConnection connection, Action<T> action)

Example:

_connection.Subscribe<ReceiveMessage>(data =>
{
    var newMessage = $"{data.User}: {data.Message}";
    Messages.Add(newMessage);
});

Without EasyUse, the same subscription would look like this:

_connection.On<string, string>("ReceiveMessage", (user, message) =>
{
    var newMessage = $"{user}: {message}";
    Messages.Add(newMessage);
});

Get a Sample Project

If you'd like to explore a working project using this framework, check out these sample applications:

Communication

Suggestions and feedback are welcome. Feel free to reach out via Telegram.